Search code examples
jsonjsonpathjson-path-expression

JSONPath Expression to return objects within an array based on value of a key


I have a JSON Array which contains multiple JSON objects:

[
   {
      "s3_uri":"s3://fake-s3-bucket/fact_table/fact_table.csv",
      "dataset":"fact_table"
   },
   {
      "s3_uri":"s3://fake-s3-bucket/dimension_table/dimension_table.csv",
      "dataset":"dimension_table"
   }
]

I would like to filter the JSON using a JSONPath Expression based on the "dataset" key value of "dimension_table" and return all the objects in the array which match this search criteria.

This is the desired filter result.

[
   {
      "s3_uri":"s3://fake-s3-bucket/dimension_table/dimension_table.csv",
      "dataset":"dimension_table"
   }
]

I have attempted to use a filter $[*][?(@.dataset==dimension table)] but this does not maintain the structure of the object and we lose the keys of the object:

[
  "s3://fake-s3-bucket/dimension_table/dimension_table.csv",
  "dimension_table"
]

Is this possible to achieve with a JSONPath Expression?


Solution

  • The following works on https://jsonpath.com:

    $[?(@.dataset=='dimension_table')]