Jacques Tardie
Jacques Tardie

Reputation: 595

Filtering GeoJSON with JQ

Given this 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
        ]
      }
    }
  ]
}

I'd like to use jq to filter through and select the features that possess the MODE: D property. As far as I can tell, the query jq .[] | select(.MODE == "D") should work, but it doesn't!

What am I missing?

Thanks in advance.

Upvotes: 4

Views: 1175

Answers (2)

gdoubleod
gdoubleod

Reputation: 1308

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

The question mark tells jq to ignore errors. The .. is to recurse into the object

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

Will get you the results you are after without recursion just to note the differences in the methods

for reference: https://github.com/stedolan/jq/issues/610

Upvotes: 2

Jeff Mercado
Jeff Mercado

Reputation: 134571

You're missing quite a lot. You used .[] but what is that supposed to accomplish? The MODE is a property of the properties object of a feature.

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

Upvotes: 2

Related Questions