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.

priyas
  • 415
  • 1
  • 8
  • 29

2 Answers2

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.

Learner
  • 209
  • 2
  • 12
  • 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 ? – priyas Dec 27 '17 at 03:09
  • **[: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. :) – Learner Dec 27 '17 at 04:27
  • @priyas - I found **[*]** to be working as expected instead of hard coding **[:100]**. I've updated the answer accordingly. Please check. – Learner Dec 27 '17 at 04:43
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.

Happs
  • 115
  • 7