You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jq为GeoJSON的properties添加新键值对的方法求助

解决方法

你的问题出在原命令直接替换了整个properties对象,而非在原有对象中添加新键值对。正确做法是定位到每个feature的properties字段,将新键值对合并进去。

正确的jq命令

jq --arg dd "$d" '.features[].properties |= . + {date_added: $dd}' /tmp/file-$d.geojson

命令说明

  • .features[].properties: 精准定位到每个feature下的properties对象
  • |= . + {date_added: $dd}: 将新的date_added键值对合并到原properties对象中,完整保留所有已有字段

执行后结果

执行该命令后,每个feature的properties会保留原有字段并新增date_added键,结果如下:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                4.991669,
                52.324436,
                0
              ]
            ]
          ]
        ]
      },
      "properties": {
        "neighbourhood": "Bijlmer-Oost",
        "neighbourhood_group": null,
        "date_added": "2022-01-01"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                5.079162,
                52.388654,
                0
              ]
            ]
          ]
        ]
      },
      "properties": {
        "neighbourhood": null,
        "neighbourhood_group": null,
        "date_added": "2022-01-01"
      }
    }
  ]
}

内容的提问来源于stack exchange,提问作者csl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:32:45