jquery - String search with jsonpath -
i have huge json file more 20000 objects in it. query string in json file. however, using jsonpath
, find exact matches?
let's file like:
{ "store": { "book": [ { "category": "reference", "author": "nigel rees", "title": "sayings of century", "price": 8.95 }, { "category": "fiction", "author": "evelyn waugh", "title": "sword of honour", "price": 12.99 }, { "category": "fiction", "author": "herman melville", "title": "moby dick", "isbn": "0-553-21311-3", "price": 8.99 }, { "category": "fiction", "author": "j. r. r. tolkien", "title": "the lord of rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 19.95 } } }
what is:
jsonpath(data, "$..book[?(@.title 'ord')]")
and books having "ord" in title.
{ "category": "fiction", "author": "j. r. r. tolkien", "title": "the lord of rings", "isbn": "0-395-19395-8", "price": 22.99 } { "category": "fiction", "author": "evelyn waugh", "title": "sword of honour", "price": 12.99 }
is possible?
update:
according the unit tests , this answer, can embed regex literal in expression , evaled. following example unit test.
$.menu.items[?(@ && @.label && /svg/.test(@.label))].id
so, example, be:
$.store.book[ ?( @ && @.title && /ord/.test(@.title) ) ].isbn
i've used jsonpath pretty large json files (2-3mb) , cause of our application slow down. if you're concerned @ performance, i'd suggest creating supplementary index each field you'd search on. doing allows weight relevance value of each field, pretty important getting search results.
or better yet, if anticipate application may grow, may want entertain server-side search engine (http://sphinxsearch.com/ or saas version http://indexden.com/).
as specific problem, operator supposed supported. however, i've been able achieve pretty performance array of strings (eg. titles in data structure) , using plain javascript regex (http://www.w3schools.com/jsref/jsref_obj_regexp.asp).
Comments
Post a Comment