2014-11-04 55 views
2

鉴于这种JSON过滤以GeoJSON有JQ

{ 
    "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")应该工作,但它不!

我错过了什么?

在此先感谢。

回答

2

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

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

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

会得到你,你是没有经过递归的结果只是要注意在方法

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

1

你”重新错过了很多。您使用了.[],但是这是否应该完成? MODE是某个功能的properties对象的属性。

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