6

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.

user2413561
  • 111
  • 1
  • 1
  • 4

3 Answers3

11

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.

riyasvaliya
  • 745
  • 6
  • 8
  • 1
    For me Tags[?(@ == 'Indian')] != null worked (null instead of []). I'm using Newtonsoft.Json.Linq.JToken in C# if anyone may find it useful. – Mariusz Schimke Jul 23 '19 at 10:55
6

Assuming you are using Goessner JSONPath (http://goessner.net/articles/JsonPath/) the following should work:

$.Books.History[?(@.Tags.indexOf('Indian') != -1)]

According to the Goessner site, you can use underlying JavaScript inside the ?() filter. You can therefore use the JavaScript indexOf function to check if your Tags array contains the tag 'Indian'.

See a working example here using this JSONPath query tester: http://www.jsonquerytool.com/sample/jsonpathfilterbyarraycontents

Duncan
  • 781
  • 6
  • 7
  • This works with the Java port (https://github.com/jayway/JsonPath) as well. Tested with version 2.0.0. – Rüdiger Schulz May 27 '15 at 14:19
  • Hey @Duncan, Is it possible to check if any string value in jsonPath repeated, could you please help me here http://stackoverflow.com/questions/40954727/check-if-any-value-in-jsonpath-is-repeated – Swapnil Kotwal Dec 04 '16 at 02:24
  • @RüdigerSchulz: I tried the exact JSON above with the solution suggested, on Jayway JsonPath, version 2.0.0 and it did NOT work. It does not give an error but it does not return any values. On version 2.3.0, it gives an exception, " 'indexOf' are not closed properly", which is a bug. – Aditya K May 21 '18 at 23:45
0

Did you try to use underscoreJS ? You can get the Indian books like this :

var data = {"Books:"....};

var indianBooks = _.filter(data.Books.History, function(book) { return _.contains(book.Tags, "Indian"); })
FredMi
  • 34
  • 2