Is there a JSON equivalent of XQuery/XPath? Is there a JSON equivalent of XQuery/XPath? json json

Is there a JSON equivalent of XQuery/XPath?

Yup, it's called JSONPath:

It's also integrated into DOJO.

JMESPath is quite a mature library with a detailed specification and support for multiple languages.

Syntax Examples:
// Select a single itempeople[1].firstName// Select a slice of an arraypeople[0:5]// Select all the first namespeople[*].firstName// Select all first names based on search termpeople[?state=='VA'].firstName// Count how many people are over 35length(people[?age>`35`])// Select only the name and age of people over 35people[?age>`35`].{name: name, age: age}// Join expressions together to sort and join elements into a stringpeople[?state == 'WA'].name | sort(@) | join(', ', @)

There are plenty more live examples you can play with in the docs.

The JS library is 19kb minified, so possibly larger than some, but considering the extensive features, you might find it worth it.

Other Options

There are also some other options for traversing/filtering JSON data, with provide some syntax examples to help compare...

  • JSPath
    .automobiles{.maker === "Honda" && .year > 2009}.model

  • json:select() (inspired more by CSS selectors)
    .automobiles .maker:val("Honda") .model

  • JSONPath (inspired more by XPath)

I think JSONQuery is a superset of JSONPath and thus replaces it in dojo. Then there's also RQL.

From Dojo documentation:

JSONQuery is an extended version of JSONPath with additional features for security, ease of use, and a comprehensive set of data querying tools including filtering, recursive search, sorting, mapping, range selection, and flexible expressions with wildcard string comparisons and various operators.

JSONselect has another point of view on the question (CSS selector-like, rather than XPath) and has a JavaScript implementation.