JsonPath Conditionals Nested Two Arrays Deep

77 views Asked by At

I'm trying to return the full parent object when the url = 'www.fruits.com'

Below is the json schema. It's quite tricky

{
  "produce": [
    {
      "type": "fruit",
      "upc": {
        "code": {
          "codingList": [
            {
              "item": "apple",
              "organizations": [
                {
                  "url": "www.fruits.com"
                }
              ]
            }
          ]
        }
      }
    },
    {
      "type": "fruit",
      "upc": {
        "code": {
          "codingList": [
            {
              "item": "tomato",
              "organizations": [
                {
                  "url": "www.special-fruits.com"
                }
              ]
            }
          ]
        }
      }
    }
  ]
}

Any help would be much appreciated.

i've tried several different method on jsonpath.com but have not been able to crack it.

1

There are 1 answers

0
gregsdennis On

Conditionally returning the root value isn't supported in JSON Path. The only way to get the root is with the path $, which doesn't allow conditions.


Assuming that you mean that you want the items in the parents that have a url with value www.fruits.com, you can use this:

$.produce[?(@..[?(@.url == 'www.fruits.com')])]

You can test this on https://json-everything.net/json-path, which implements the upcoming IETF spec for JSON Path.

https://jsonpath.com/ doesn't seem to support @ very well. It appears that it requires a subquery, e.g. @.foo.