【问题标题】:Filtering GeoJSON with JQ使用 JQ 过滤 GeoJSON
【发布时间】:2014-12-31 16:38:10
【问题描述】:

鉴于此 JSON

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "MODE": "A"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -69.23583984375,
          45.460130637921004
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "MODE": "D"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -69.23651039600372,
          45.46053888199693
        ]
      }
    }
  ]
}

我想使用jq 过滤并选择拥有MODE: D 属性的features。据我所知,查询jq .[] | select(.MODE == "D") 应该可以工作,但不能!

我错过了什么?

提前致谢。

【问题讨论】:

    标签: javascript json filter geojson jq


    【解决方案1】:

    jq ' .. | select( has("properties") )? | select( .properties.MODE == "D")'

    问号告诉 jq 忽略错误。 .. 是递归到对象中

    jq '.features[] | select(.properties.MODE == "D")'

    无需递归即可获得您想要的结果,只是为了注意方法的差异

    供参考:https://github.com/stedolan/jq/issues/610

    【讨论】:

      【解决方案2】:

      你错过了很多。您使用了.[],但这应该完成什么? MODE 是特征的 properties 对象的属性。

      .features | map(select(.properties.MODE == "D"))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-20
        • 2017-04-16
        • 2014-01-20
        • 2018-11-05
        • 2015-08-26
        • 1970-01-01
        • 2022-11-15
        相关资源
        最近更新 更多