JSONPath or other XPath like utility for JSON/Javascript; or Jquery JSON

只谈情不闲聊 提交于 2019-11-30 02:36:17

Check out JSON Select - CSS-like selectors for JSON.

Definitely would be a useful utility.

My opinion is that the best way to approach this would be to stay as similar as possible to css selectors, as you indicate. I'd recommend looking under the hood at jquery's implementation of selectors.

I would suggest something like

var banana = object.function(jsonObect, "holding #Banana");
var leftHands = object.function(jsonObject, "appendage[type=hand][side=left]");

instead of your usage examples.

I'm not sure how the upcoming native json support will affect this...

OK, I have created a prototype for this, available here: http://code.google.com/p/jfunk/

It has already proven useful for me, so I will probably slowly improve and refactor it into something nice. But if I get good feedback, I can move more quickly. I would also welcome help.

Try to using JSPath -- https://github.com/dfilatov/jspath.

JSPath is a domain-specific language (DSL) that enables you to navigate and find data within your JSON documents. Using JSPath, you can select items of JSON in order to retrieve the data they contain.

JSPath for JSON like an XPath for XML.

Dojo's dojo.getObject has a facility that works loosely like this, where you can provide a path like "a.b.c" to the property you want to fetch.

Check it out:

http://api.dojotoolkit.org/jsdoc/1.3/dojo.getObject

I don't think it understands arrays quite that well and I think it is missing a full-featured selector language like the one you are suggesting.

As for usage, I've coded a selector language like the one you are suggesting, but for a client, and array addressing is very proprietary to their particular object structure.

I would definitely use a system like this if you were to make it, and perhaps even contribute if I saw an area I could help with.

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.

Looks like there's a new option: jQuery-JSONPath. Seems to be exactly what you're asking for.

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