2

I am trying to filter some data from the geoJSON data structure shown as below:

 "features": [
 {
      "type": "Feature",
      "properties": {
        "@id": "node/7071544593",
        "addr:city": "Joensuu",
        "addr:housenumber": "12",
        "addr:postcode": "80100",
        "addr:street": "Siltakatu",
        "addr:unit": "C 33",
        "alt_name": "Crasman Oy Joensuu",
        "alt_name_1": "Crasman Oy",
        "name": "Crasman Joensuu",
        "short_name": "Crasman",
        "website": "https://www.crasman.fi"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          29.7621398,
          62.6015236
        ]
      },
      "id": "node/7071544593"
    },
    {
      "type": "Feature",
      "properties": {
        "@id": "node/7117872562",
        "amenity": "car_rental",
        "operator": "avis"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          29.7630643,
          62.6036656
        ]
      },
      "id": "node/7117872562"
    }
]

What I am trying to do is iterate through this array of features, look into the properties object to check if it contains website, if Yes, then I can print its coordinates from geometry object. This is what I tried:

Features[*].properties[?contains(@,'website')=='true'].geometry.coordinates

It gives me null value

Mohsin
  • 73
  • 2
  • 9

1 Answers1

0

Try this:

features[?contains(keys(properties),'website')].geometry.coordinates

E.g.:

$ jp "features[?contains(keys(properties),'website')].geometry.coordinates" <input.json
[
  [
    29.7621398,
    62.6015236
  ]
]

With regard to why your example didn't work:

  • Identifiers are case-sensitive, so you need features, not Features.
  • properties is an object, not an array, so you can't apply a filter expression to it.
  • Even if you could, it's not properties that you want to filter. You are trying to filter whole features.
  • contains tests if an array contains an item (or if a string contains a substring), not whether an object has a key. You can use keys() to get the keys of an object in an array.
  • You don't need to compare the result of contains() to true, it's already a boolean.
  • Even if you were trying to compare to true, you'd need to use backticks: `true`, not quotes 'true'.
Weeble
  • 17,058
  • 3
  • 60
  • 75