1.9.07 # JSONPath - XPath for JSON

Here and there on the web we can find a discussion or solution for something like XPath for JSON. Despite the fact, that JSON structures can be processed by the C family of programming languages quite easily, there should be a benefit in having some kind of XPath4JSON.

So I was thinking about a suitable path expression syntax for addressing portions of any JSON structure. Those path expressions should

  • be naturally based on C language family characteristics.
  • cover only essential parts of XPath 1.0.

As a result here is JSONPath, wich is a dual thing. Primarily JSONPath is a path expression syntax, which can select parts of JSON structures in the same way as XPath expressions select nodes of XML documents. Such an expression may look like

$..book[2].title

and is quite similar to its XPath counterpart

//book[3]/title

Currently there is a small set of nine JSONPath syntax elements, which allows the construction of sophisticated path expressions.

Furthermore JSONPath is a lightweight tool implemented in Javascript and PHP for accessing JSON structures on the client and the server.

You can download

for free and read more about it.

Update:

Atif Aziz came up with a C# port of JSONPath in a very short time (jsonpath.cs). Thanks for that.

Labels: , , ,

3 comments

This is a very useful library to fetch specific elements from PHP arrays. It works very well, however I am missing one critical feature. I need to be able to escape periods (.) in the node names/path.
For example $['node\.name'] does not work in selecting the "node.name" key in the array. Any chance that basic escaping could be added?
 

Good point. As you enclose the key in single quotes, it should even work without escaping, i.e. $['node.name'] . I wrote an issue regarding this and resolved the bug with version 0.8.3.

Actually there is no escaping mechanism implemented (theoretically necessary only for [' ; #]).
 

Is there a jsonpath specification site where to discuss about jsonpath specification?
I've a java project that implements JSONPath but I've some doubt
 
Kommentar veröffentlichen