Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

filter the Json according to string in an array in JSONPATH

I have a situation where I have json String that has a child as Array that contains only Strings. Is there as way I can get the object reference of the arrays that contains a specific String. Example:

{ "Books":{  
      "History":[  
         {  
            "badge":"y",
            "Tags":[  
               "Indian","Culture"
            ],
            "ISBN":"xxxxxxx",
            "id":1,
            "name":"Cultures in India"
         },
         {  
            "badge":"y",
            "Tags":[  
               "Pre-historic","Creatures"
            ],
            "ISBN":"xxxxxxx",
            "id":1,
            "name":"Pre-historic Ages"
         }
     ]
  }
}

To Achieve: From the above JSON String, need to get all books in History which contains "Indian" inside the "tags" list.

I am using JSONPATH in my project but If there is other API that can provide similar functionality, any help is welcome.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you're using Goessner JSONPath, $.Books.History[?(@.Tags.indexOf('Indian') != -1)] as mentioned by Duncan above should work.

If you're using the Jayway Java port (github.com/jayway/JsonPath), then $.Books.History[?(@.Tags[?(@ == 'Indian')] != [])] or more elegantly, use the in operator like this $.Books.History[?('Indian' in @.Tags)]. Tried them both here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...