metawops
metawops

Reputation: 387

How to filter for a whole dictionary by a specific value inside it via JSONPath?

All I (just) leardned about JSONPath is that I can filter/select for parts of a JSON string by giving key names or array indices etc. The result is that key/value I gave in the JSONPath expression. However, I want to get, as a result, the whole dictionary where inside of it is a key/value pair I want to filter for – not only that key/value pair.

For example, imagine the following JSON string:

[
 {
  „date“: „2021-06-18T10:23:45.000+0000“,
  „material“: {
    „key1“: „value1“,
    „key2“: „value2“
  },
  „preset“: {
    „duration“: [ 120, 180 ],
    „increments“: 3,
    „id“: „16“,
    „uniqueId“: „1357924680“
  },
  „userId“: 23456
 },
 {
  „date“: „2021-06-18T08:15:45.000+0000“,
  „material“: {
    „key1“: „value1“,
    „key2“: „value2“
  },
  „preset“: {
    „duration“: [ 180 ],
    „increments“: 1,
    „id“: „10“,
    „uniqueId“: „1797924680“
  },
  „userId“: 23456
 },
 {
  „date“: „2021-06-16T17:12:23.000+0000“,
  „material“: {
    „key1“: „value1“,
    „key2“: „value2“
  },
  „preset“: {
    „duration“: [ 240 ],
    „increments“: 1,
    „id“: „10“,
    „uniqueId“: „1357944680“
  },
  „userId“: 23456
 },
 {
  „date“: „2021-06-09T08:45:45.000+0000“,
  „material“: {
    „key1“: „value1“,
    „key2“: „value2“
  },
  „preset“: {
    „duration“: [ 120, 180 ],
    „increments“: 3,
    „id“: „16“,
    „uniqueId“: „2357924680“
  },
  „userId“: 23456
 }
]

Now suppose I want to get those array elements (completely, with all their data) where inside the „preset“ named dictionary the value of „id“ is equal to „16“. What‘s the JSONPath query string for this? Remember: I don‘t want to get just { „id“: „16“, „id“: „16“ } as a result but instead the whole dictionary with both results in this case:

 {
  „date“: „2021-06-18T10:23:45.000+0000“,
  „material“: {
    „key1“: „value1“,
    „key2“: „value2“
  },
  „preset“: {
    „duration“: [ 120, 180 ],
    „increments“: 3,
    „id“: „16“,
    „uniqueId“: „1357924680“
  },
  „userId“: 23456
 },
 {
  „date“: „2021-06-09T08:45:45.000+0000“,
  „material“: {
    „key1“: „value1“,
    „key2“: „value2“
  },
  „preset“: {
    „duration“: [ 120, 180 ],
    „increments“: 3,
    „id“: „16“,
    „uniqueId“: „2357924680“
  },
  „userId“: 23456
 }

And I don‘t want to do this inside some coding project/a program/source code in any programming language. Instead I have a REST API tool where I can filter JSON responses with a JSONPath query string (it‘s the app „HTTPBot“ for i[Pad]OS).

Here are a few JSONPath examples I tried but don‘t give the result I want:

Upvotes: 0

Views: 1819

Answers (1)

gregsdennis
gregsdennis

Reputation: 8428

$..preset is going to perform a recursive search for any property (at any depth) called preset and return the collection of all the values in those properties.

To get the items, you need to have a selector that acts on them. Since you want to filter the items, you're looking at the filter selector [?(<expr>)].

Within JSON Path expressions, you can use the @ to look at each item as the selector iterates the array, and it supports building a path from it. We want the @.preset.id to be 16, so we can use

$[?(@.preset.id=='16')]

Upvotes: 1

Related Questions