dunfa
dunfa

Reputation: 539

JSON query with JsonPath by sibling node condition

This is a problem I am having for JSON parsing with JsonPath. I do come up with a solution but not sure if it is the best/only.

Suppose I have a simple JSON like this,

{
   "name": "Bill",
   "age" : 33
}

I need to select the "name" node if the value of "age" node is less than 40. If it is not, just return an empty array.

Could anyone share your solution? I have mine as one answer below.

Upvotes: 0

Views: 1235

Answers (1)

dunfa
dunfa

Reputation: 539

First I have to use $. to turn the JSON into an array of JSON, ie,

        {
          "name": "Bill",
          "age" : 33
        }

to

        [
            {
              "name": "Bill",
              "age" : 33
            }
        ]

Then I can apply $[?(@.age < 40)].name

Upvotes: 2

Related Questions