Filter Json by string in array in JSONPATH

I have a situation where I have a json String that has a child as an array containing only strings. Is there a way to get a reference to an object of arrays containing 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"
         }
     ]
  }
}

Reach: From the above JSON line, you need to get all the books in History that contain "Indian" in the "tags" list.

I use JSONPATH in my project, but if there is another API that can provide similar functionality, any help is appreciated.

+2
source share
2 answers

, Goessner JSONPath (http://goessner.net/articles/JsonPath/), :

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

, JavaScript ?(). JavaScript indexOf, , 'Indian'.

. , JSONPath: http://www.jsonquerytool.com/sample/jsonpathfilterbyarraycontents

+3

underscoreJS? :

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

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

Source: https://habr.com/ru/post/1665610/


All Articles