2014-11-04 96 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"))