Home > database >  JSONPath expression for an array of multiple arrays
JSONPath expression for an array of multiple arrays

Time:01-11

I have a json payload that looks like this

{
   "data":{
      "methods":[
         [
            {
               "p_id":"01",
               "description":"Test01",
               "offline":true
            }
         ],
         [
            {
               "p_id":"02",
               "description":"Test02",
               "offline":false
            }
         ],
         [
            {
               "p_id":"03",
               "description":"Test03",
               "offline":true
            }
         ]
      ]
   }
}

How can I write a JSONPath expression to get the "p_id" where "offline"= false?

CodePudding user response:

You can use a filter expression which selects all elements in an object or array that match the specified filter. For example, [?(@.offline === false)] will match any object if its offline property is strictly false.

So, if the object is always in the same place, you could do:

$.data.methods.*[?(@.offline === false)].p_id

Or, if you want to look for any object where offline is false and fetch p_id, you could use a recursive descent with the filter expression:

$..[?(@.offline === false)].p_id

Note: I used strict equality in my examples so it will only match with a boolean false. If you don't need/want that you could instead simply use a ! to negate the filter. E.g. [?([email protected])]

  • Related