Is it possible to perform complex queries over a JSON object? I am open to JavaScript or jQuery solutions, the easier the better. I'm envisioning some kind of functional programming language similar to LINQ or SQL.
I Prefer no other third party libraries or add-ons.
UPDATE
From the looks of early answers, an add-on is going to be necessary. In that case, I prefer an add-on that requires no installation process. Something that deploys with the software publish (like jQuery) is fine (e.g. sets of *.js files).
By the time you're interacting with it, it's not a "JSON object," it's a JavaScript object. ("JSON objects" only exist in terms of the data notation.) JavaScript itself doesn't have any advanced functional programming constructs, so you'd need third party libraries for that sort of thing. JavaScript pretty much just has property accessors, an operator for "does this object have a property with this name?" (
in
,hasOwnProperty
), and as of the 5th edition (not yet widely supported), some handy array-specific features likeforEach
,every
,map
,filter
, and the like.hmm... YQL does this sort of thing, but that would be a third party.
you could filter an array with the jQuery
$.grep(array,filterfn)
methodand you can of course use a regexp in there if you wish, or use more complex conditions, such as checking multiple keys, keys within keys, arrays within keys, etc.
Use
JSON.stringify
and areplacer
callback to achieve this:Here is some documentation on the two JSON methods for serialization and transformation,
stringify
andparse
:JSON.parse(source, reviver)
JSON.stringify(value, replacer, space)
References
ECMAScript Wiki:JSON Support
MDN:JSON.parse Examples
MDN:JSON.stringify toJSON behavior
MSDN:toJSON Method
Opera:JSON.parse
Check out: Is there a query language for JSON?
From that thread:
JaQL(Wiki)
JsonPath.
Json Query