0

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
  • By "full parent object", do you mean you want { "produce": ... } or do you want the item in the parents array? Commented Nov 6, 2023 at 4:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.