views:

663

answers:

3

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?

+2  A: 

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

cofiem
Yes, passing in the object will make sense. I did some hacking on this last night and am encouraged by the possibilities.... more to come!
larson4
Please check out my answer, I have created a prototype
larson4
+1  A: 

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.

Maciek
I hadn't found this one, interesting. I DEFINATELY feel the pain behind "Useful for longer api chains where you have to test each object in the chain", as I've many times had to write junk like var a=(b ? c ? d ? b.c.d : null : null : null); [though usually much uglier and including other conditions]. Still, this seems underwhelming overall... OK, definitely adding weight to creating a parallel Jquery-like library for Json/JavascriptObjects, though there will be obvious necessary differences, and I doubt performance will be as good...
larson4
Please check out my answer, I have created a prototype
larson4
+1  A: 

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.

larson4
nice. I just had a quick glance over the code. certainly got the core of the idea there - might be some possible tweaks to make it more efficient, but seems to work ok just now. I'll have a look at it in a week or two when I have some more time.
cofiem
yes, didn't have time to even think about efficiency-- other than to realize that DOM has some real advantages here._but_ whatever tasks this is useful for may not be able to be efficiently implemented any other way... though that would argue for redesigning data... oh well, i think this will be useful
larson4