1

I have a JSON Structured like this :

[{
  "firstName": "John",
  "age"      : 26,
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
},
{
  "firstName": "Johny",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}] 

I want to extract the users who have a Iphone and name is JOHN .

I have used below expression

$[?(@.firstName=='John')].phoneNumbers[?(@.type=='iPhone')] 

But I want to extract the complete user information . I have tried Filter Criteria API as well , but In it I am not able to find a way to access Phone Type attribute.

2 Answers 2

1

As mentioned in this post, the Jayway implementation supports inlined AND and OR criteria. The following JSON Path should meet your requirements.

$[?(@.firstName=='John' && 'iPhone' in @.phoneNumbers[*].type)]

Below is screenshot from Jayway JsonPath Evaluator

Jayway JsonPath Evaluator

Also, please be informed that the syntax may vary depending on the implementation used.

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

3 Comments

thanks for your response. That's exactly I was looking for . Though I am not aware what this phoneNumbers[:100] is significant for . Can you please explain ?
[:100] means the first hundred phone numbers of the person. Assuming a person cannot have more than hundred phone numbers, this should work. It should ideally be the actual number of phone numbers of a person, which I could not get from jsonpath syntax for some reason. :)
@priyas - I found [*] to be working as expected instead of hard coding [:100]. I've updated the answer accordingly. Please check.
0
for(var i=0;i<s.length;i++){
  for(var j=0;j<s[i].phoneNumbers.length;j++){
    if(s[i].phoneNumbers[j].type == 'iPhone'){
        alert(s[i].firstName+" "+s[i].age+" "+s[i].phoneNumbers[j].type+" 
              "+s[i].phoneNumbers[j].number);
    }
  }
}

Here var s is your json object.

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.