JSONPath or other XPath like utility for JSON/Java

2019-02-02 02:08发布

问题:

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? What would be really slick is if there was a plugin for JQuery that did something like this. I have been searching the plugins and coming up empty handed. Anyway, before I spend time getting to know JSONPath (which has some aspects I am not keen on), or before I reinvent wheels, I thought I'd see if anyone had an angle on this...

To give you an idea what I mean, imagine this Javascript object:

var Characters=[
        {
            id: "CuriousGeorge",
            species:"Monkey",
            mood: "curious",
            appendage: [
                { 
                    type: "hand",
                    side: "left",
                    holding: [ 
                        { id: "Banana" } 
                    ]
                },
                { 
                    type: "hand",
                    side: "right",
                    holding: []
                }, 
                { 
                    type: "foot",
                    side: "left",
                    holding: []
                },
                { 
                    type: "foot",
                    side: "right",
                    holding: [ 
                        { id: "YellowHat" },
                        { id: "Keys" }
                    ]
                }
            ]
        },
        {
            id: "ManInYellowHat",
            species: "Human",
            mood: "angry",
            //...ok, you get it...
        }
    ]

Wouldn't it be great to get to of some of the deeper objects by something like Jquery selectors?

var banana=SomeUtility("Characters holding #Banana").get(0);
var leftHands=SomeUtility("Characters appendage[type=hand][side=left]").get();

(This may qualify for worlds corniest code example, but hey, my kids just watched this. And I can't use real example because of NDA...)

...And, to make it more interesting, if I were to create such a thing, would anyone use it?

回答1:

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



回答2:

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...



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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