JSON

Developing the next generation of open data interchange

« PreviousNext »

Extending JSONPath

10 June 2008

I would like to add some additional capabilities to the JSONPath implementations that I have worked on: Dojo’s dojox.jsonPath and Persevere’s database querying.

Lenient syntax handling

Right now a simply JSONPath query using a filter expression looks like:

$[?(@.price < 10)]

We could first of all remove the requirement to start with $, and just let it start with an operator ([ or .). Next, it doesn't seem necessary to require a parenthesis to enclose the expression following the ? on filter expression. We could have

[?@.price<10]

Then we could also imply that any direct names in a filter expression are properties of @ (the filter items):

[?price<10]

Finally, we don’t really even need the brackets except when more query operations will follow the filter expression:

?price<10

If we had another operation, queries would have to retain the brackets:

[?price<10][0:10]

Sorting

I would like to use syntax like:

[<firstName]

to indicate sorting ascending by firstName. A query might look like:

$[?price < 10][>rating]

to find items under $10 and then sorting by rating (highest first)
Multiple prioritized columns of sorting could be done like:

$[?price < 10][>rating, <author.firstName]

which would sort first by rating (descending) and then ties in rating would be sorted by author’s firstName in ascending order.

Multi-Column/Property Selection

Right now you can select a single property to extract from a list of objects:

$[?price < 10].author

Creates a list of authors who have a book for under $10. But we need a new mechanism for multiple properties:

$[?price < 10]{rating:rating, name:author.firstName + ‘ ‘ + author.lastName}

This will create a list of objects that have a rating property (from the book’s rating), and a name property that is the concatenation of the author’s first and last name.
It might be convenient if specifying a value in the key:value sequence was optional:

$[?price < 10]{rating, name}

Returns objects with the book’s rating and name properties.

General result-level expressions

It could also be useful if our queries could also handle general result-level expressions:

$[0].rating > 3

Finds the rating for the first book and returns true or false based on the rating being greater than 3.

I am also wondering with all these new capabilities (I previously added “safe evaluation” and result-based evaluation) that have been added to JSONPath, if it would be better off with a new name. This enhanced querying technology could be called JSONQuery, noting that it supersets JSONPath (for the most part), as well as nicely intersects JavaScript. This may be a better route than adding extra capabilities to JSONPath engines that do not have any specification that reflects these extra capabilities.

What do you think, are these new capabilities valuable? Should it be called JSONQuery, or just JSONPath + additional features?

Posted in Libraries | Trackback | del.icio.us | Top Of Page

No comments yet

Leave a Reply