json - 使用 JQ 将 Geojson 附加到 json 字段

标签 json jq

我正在开发一个项目,该项目创建一个分区统计图,其中包含从 file1.json 加载的所有美国县边界,并根据 file2.json 中的值填充颜色渐变。在之前的迭代中,我只是手动将值输入到 file1.json 中,但现在我想扩展我的 map 并使其更加用户友好。

file1.json 的结构如下:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "GEO_ID": "0500000US06001",
        "STATE": "06",
        "COUNTY": "001",
        "NAME": "Alameda",
        "LSAD": "County",
        "CENSUSAREA": 739.017
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.30936,
              37.77615
            ],
            [
              -122.317215,
              37.778527
            ]
          ]
        ]
      }
    },
    ...
  ]
}

file2.json 的结构如下:

[
  {
    "County": "Alameda",
    "Count": 25
  },
  {
    "County": "Amador",
    "Count": 1
  },
  {
    "County": "Butte",
    "Count": 2
  },
  ...
]

我想创建一个包含 file1.json 中所有内容的新文件,但将其附加到基于“县”字段的相关事件计数字段。

结果将如下所示:

[
{
  "type": "Feature",
  "properties": {
    "GEO_ID": "0500000US06001",
    "STATE": "06",
    "COUNTY": "001",
    "NAME": "Alameda",
    "Count": "25",
    "LSAD": "County",
    "CENSUSAREA": 739.017
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -122.30936,
          37.77615
        ],
        [
          -122.317215,
          37.778527
        ]
      ]
    ]
  }
},
...
]

我刚开始使用 jq,但我已经充分尝试过它,可以让它在 PowerShell 中运行。

最佳答案

这是一个可能有帮助的test.jq文件

# utility to create lookup table from array of objects
# k is the name to use as the key
# f is a function to compute the value
#
def obj(k;f): reduce .[] as $o ({}; .[$o[k]] = ($o | f));

# create map from county to count
  ( $file2 | obj("County";.Count) ) as $count

# add .properties.Count to each feature
| .features |= map( .properties.Count = $count[.properties.NAME] )

假设合适的 file1.jsonfile2.json 的示例使用:

$ jq -M --argfile file2 file2.json -f test.jq file1.json
{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "GEO_ID": "0500000US06001",
        "STATE": "06",
        "COUNTY": "001",
        "NAME": "Alameda",
        "LSAD": "County",
        "CENSUSAREA": 739.017,
        "Count": 25
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.30936,
              37.77615
            ],
            [
              -122.317215,
              37.778527
            ]
          ]
        ]
      }
    }
  ]
}

Try it online!

我注意到“Count”在示例输出中是一个字符串,但在示例文件2中却是一个数字。如果您需要将其转换为字符串,您可以调用 tostring。例如

.features |= map( .properties.Count = ( $count[.properties.NAME] | tostring ) )

或者您可以在创建查找表时执行转换,例如

  ( $file2 | obj("County"; .Count | tostring ) ) as $count

关于json - 使用 JQ 将 Geojson 附加到 json 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64469597/

相关文章:

arrays - 如何使用 jq 将 JSON 文件分解为包裹在数组中的较小 json?

json - 如何将现有 JSON 文件的内容包装为对象的一部分?

json - 使用 jq 将 json 转换为 csv,数组嵌套在数组中

javascript - 通过查找值 AngularJS 在数组中添加键

javascript - 如何访问Json数据?

json - 如何使用jq将两个文件中的数组合并为一个数组?

json - 使 JQ 输出一张表

javascript - 如何从此 JSON 数组中删除不需要的元素?

java - JSON:Python 输出文件到 Java ObjectMapper - 大小错误

Python Validictory(JSON 模式验证): How to OR multiple schemas?