Search code examples
javajsonpath

JSONPath get value from map within a table


I have a JSON string as follows (simplified):

{
   "data":{
      "fruit":[
         {
            "role":[
               {
                  "role":"passive"
               }
            ],
            "objectType":"apple"
         },
         {
            "role":[
               {
                  "role":"active"
               }
            ],
            "objectType":"orange"
         }
      ]
   }
}

I would like to get objectType value where role is active. In this example the result would be orange. I've come up with the following code, however it doesn't return anything. How do I fix this?

$.data.fruit[?(@.role.role == 'active')]

Note that the number of elements in fruit array may vary and there is no guarantee that the last element would be correct.


Solution

  • At OP's request, here's the final, final answer!

    $.data.fruit[?('active' in @..role..role)].objectType