filter the Json according to string in an array in JSONPATH

戏子无情 提交于 2019-12-01 11:07:08

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

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.

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"); })
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!