Search code examples
pythonjsonpath

How do I return the value of a key that is nested in an anonymous JSON block with jsonpath?


I am trying to extract the value of a key that is nested in an anonymous JSON block. This is what the JSON block looks like after result:

"extras": [
      {
        "key": "alternative_name",
        "value": "catr"
      },
      {
        "key": "lineage",
        "value": "This dataset was amalgamated, optimised and published by the Spatial hub. For more information visit www.spatialhub.scot."
      },
      {
        "key": "ssdi_link",
        "value": "https://www.spatialdata.gov.scot/geonetwork/srv/eng/catalog.search#/metadata/4826c148-c1eb-4eaa-abad-ca4b1ec65230"
      },
      {
        "key": "update_frequency",
        "value": "annually"
      }
    ],

What I am trying to do is extract the value annually but I can't use index because some other datasets have more keys under the extras section. I am trying to write a jsonpath expression that extracts value where key is update_frequency
So far what I have tried is:

$.result.extras[*].value[?(key='update_frequency')]

And still no luck.
Any idea what could be wrong?


Solution

  • This should work:

    $.result.extras[?(@.key=="update_frequency")].value