|
JpaTh
jquery.jpath.js
Inspired by xPath and jQuery selectors, jPath is an experimental plugin to allow easy browsing of JavaScript object, namely JSON objects. The goal is make it easy to pull/compare/search data from these object. Here's a simple example of how it's supposed to work. Example
// .. some ajax call get a JSON object called "res"
// the conventional way to digg for data
if (res && res.employee && res.employee.name == 'Max') {
alert(res.employee.name);
}
// with jpath
if ($(res).jpath('employee.name:is(Max)')) {
alert($(res).jpath('employee.name');
}
Furthermore, in the above example the conventional way assume that employee.name is defined. But if the server returns a JSON object and the name isn't defined the script will throw an error. jPath will simply return false if either employee or name isn't defined, making it a bit more reliable. Basic usage
var obj = {
a: {
b: 'yay',
c:[1,2,3]
}
}
$(obj).jpath('a.b'); // return "yay"
$(obj).jpath('a.b:match(ya)'); // return "true"
$(obj).jpath('a.c'); // returns [1,2,3]
$(obj).jpath('a.c:eq(2)'); // returns "3"
$.jpath.expr
| ||||||||||||||||||
► Sign in to add a comment
Dojo has an implementation of jsonpath specification (http://goessner.net/articles/JsonPath/), it can be interesting wrap it for jquery.
The JSONPath implementation can also be downloaded without Dojo packaging: http://www.sitepen.com/blog/wp-content/uploads/2008/01/jsonpath-0-8-b.js
References: http://www.sitepen.com/blog/2008/03/17/jsonpath-support/