I have been looking at JSONPath and though it seems pretty well done, I wonder if anyone has worked with it and can comment on its usability, or can recommend alternatives?
I just wrote a clientside JS-lib that does just this - it makes it possible to query a JSON structure with XPath.
@jlarson - with "defiant.js", you can query your JSON structure like this (this lib extends the global JSON object):
JSON.search( Characters, '//*[id="Banana"]' );
This call will return an array with matching nodes AND those matches won't be detached from your original JSON object (same behaviour as when working with XML + XPath). To illustrate what I mean, here is a little pseudo(-ish) code:
var store = {
"book": [
{
"id": 1,
"price": 8.95,
"title": "Sayings of the Century",
"category": "reference",
"author": "Nigel Rees"
},
{
"id": 2,
"price": 22.99,
"title": "The Lord of the Rings",
"category": "fiction",
"author": "J. R. R. Tolkien",
"isbn": "0-395-19395-8"
}
]
};
var b1 = JSON.search( store, '//book[1]' );
b1[0].isbn = '12345';
console.log( store.book[0].isbn );
// 12345
This lib is thus far for browser and clientside but I've plans to re-write it for NodeJS eventually. Check out the Xpath evaluator here; that demonstrates the functionality. There are also pre-written Xpath expressions as well:
http://defiantjs.com/#xpath_evaluator
You can find the lib at Github:
https://github.com/hbi99/defiant.js
Finally, there is a little more functionality in "defiant.js" and if you're interested, you'll hopefully read about it over there (http://defiant.com)
I hope you'll find it useful.