Home > Net >  JSONPath filter top level key with certain value
JSONPath filter top level key with certain value

Time:05-09

Consider the following JSON:

{
    "expensive": 10,
    "abc":10
}

I would like to write a filter that returns true if the key abc equals 10, i.e. on the example above the answer is true, while on the following example it is false:

{
    "expensive": 10
}

because there is no such key abc.

I have tried these filters:

  1. $[?(@ == 10)] returns [10,10] on the first json and [10] on the second, but I want to enforce the check on the abc field, i.e. the second should return nothing. how can this be achieved?

CodePudding user response:

I'm assuming you have an array of input objects and want to filter them.

[{
    "expensive": 10,
    "abc":10
}, {
    "expensive": 10
}]

Here $[?(@.abc == 10)] returns

[
  {
    "expensive": 10,
    "abc": 10
  }
]

and $[?(@.abc == 10)].expensive returns

[
  10
]
  • Related