JSONPath or other XPath like utility for JSON/Java

2019-02-02 01:29发布

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?

7条回答
Deceive 欺骗
2楼-- · 2019-02-02 02:01

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

查看更多
乱世女痞
3楼-- · 2019-02-02 02:04

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.

查看更多
够拽才男人
4楼-- · 2019-02-02 02:14

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

查看更多
乱世女痞
5楼-- · 2019-02-02 02:16

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.

查看更多
一纸荒年 Trace。
6楼-- · 2019-02-02 02:17

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.

查看更多
干净又极端
7楼-- · 2019-02-02 02:22

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

查看更多
登录 后发表回答