0

I'm trying to return the value of 'id', where value is "India" in nested JSON as shown below

{
  "iterations": [
    {
      "id": "122",
      "parameters": [
        {
          "name": "Country",
          "value": "US"
        }
      ]
    },
    {
      "id": "123",
      "parameters": [
        {
          "name": "Country",
          "value": "India"
        }
      ]
    }
  ]
}

I have already referenced similar question question asked JsonPath Conditionals Nested Two Arrays Deep

I tried

$.iterations[?(@.parameters[?(@.value=='India')])].id

$.iterations[?(@..[?(@.value == 'India')])].id

Every time the expression is returning all 2 parameter items. Any help would be much appreciated.

1 Answer 1

-1

Can you try:

$.iterations[?(@.parameters[?(@.name == 'Country' && @.value == 'India')])].id

?

Inside this inner filter, @ refers to the current parameter object. This condition checks if the name property is "Country" AND the value property is "India" for that specific parameter object.

The outer filter [?(@.parameters[?(@.name == 'Country' && @.value == 'India')]) will evaluate to true for an iteration only if its parameters array contains at least one object that satisfies the inner condition (name == 'Country' && value == 'India')

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

2 Comments

I did try that, the result remains same [ "122", "123" ] I'm using https://jsonpath.fly.dev/ to validate the jayway jsonpath result.
Yes, me too. I was using jsonpath.com The two sites gives different results. Depends on the actual implementation, I suppose.

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.