使用JQ过滤GeoJSON

时间:2014-11-04 17:13:55

标签: javascript json filter geojson jq

鉴于此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过滤并选择拥有features属性的MODE: D。据我所知,查询jq .[] | select(.MODE == "D")应该有效,但它没有!

我错过了什么?

提前致谢。

2 个答案:

答案 0 :(得分:2)

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

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

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

只是为了注意方法中的差异

,可以得到你没有递归的结果

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

答案 1 :(得分:1)

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

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