JSONPath or other XPath, as a utility for JSON / Javascript; or jquery json

I looked at JSONPath, and although it looks pretty good, I wonder if anyone has worked with it and can comment on its usability or can recommend alternatives? Which would be very smooth if there was a jQuery plugin that did something like this. I searched for plugins and came up empty-handed. Anyway, before I spend time, I get to know JSONPath (which has some aspects that I'm not addicted to), or before I invent the wheels, I thought I'd see if anyone had a corner on this. ..

To give you an idea of ​​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 some deeper objects with something like jQuery selectors?

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

(This may pretend to be the most powerful code in the world, but, hey, my kids just watched it. And I can't use a real example because of the NDA ...)

... And to make it more interesting, if I created one, would someone use it?

+14
json javascript jquery jsonpath
May 13, '09 at 16:39
source share
7 answers

Check out JSON Select - CSS selector for JSON.

+6
Dec 13 2018-11-11T13
source share

It will definitely be a useful utility.

My opinion is that the best way to get closer to this would be as close as possible to the css selectors, as you point out. I would recommend looking at the jquery implementation of selectors under the hood.

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

I'm not sure that upcoming support for native json will affect this ...

+4
May 14 '09 at 12:26
source share

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

This has already established itself as useful to me, so I will probably gradually improve it and transform it into something pleasant. But if I get good reviews, I can move faster. I also welcome help.

+4
May 17 '09 at 15:09
source share

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

JSPath is a domain language (DSL) that allows you to navigate and find data in your JSON documents. Using JSPath, you can select JSON elements to retrieve the data that they contain.

JSPath for JSON, like XPath for XML.

+3
Nov 27 '12 at 19:13
source share

Dojo dojo.getObject has a tool that works freely like this, where you can specify a path, such as "abc", to the property you want to get.

Check this:

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

I don’t think it is well versed in arrays, and I think that there is no fully functional selector language like the one you offer.

As for use, I encoded a selector language, such as the one you offer, but for the client and the addressing of the array is very proprietary to their specific object structure.

I would definitely use such a system if you made it, and perhaps even help if I saw an area in which I could help.

+2
May 14, '09 at 15:48
source share

I just wrote a client-side JS-lib that does just that - it allows you to query the JSON structure using XPath.

@jlarson - with "defiant.js", you can query the JSON structure as follows (this lib extends the global JSON object):

 JSON.search( Characters, '//*[id="Banana"]' ); 



This call returns an array with the corresponding nodes, and these matches will not be separated from the original JSON object (the same behavior as when working with XML + XPath). To illustrate what I mean, here is a small pseudo-code (-ish):

 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": "JRR 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 library is still available for browsers and clients, but I plan to rewrite it for NodeJS in the end. Check out the Xpath evaluator here; which demonstrates functionality. There are also pre-written Xpath expressions:

http://defiantjs.com/#xpath_evaluator

You can find lib on Github:
https://github.com/hbi99/defiant.js

Finally, there is a bit more functionality in defiant.js, and if you're interested, you hopefully read about it there ( <a2> )

I hope you find this helpful.

+1
Jan 02 '14 at 11:23
source share

There seems to be a new option: jQuery-JSONPath . It seems exactly what you are asking for.

+1
Aug 19 '14 at
source share



All Articles