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

jwl picture jwl · May 13, 2009 · Viewed 11.9k times · Source

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?

Answer

orad picture orad · Dec 13, 2011

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