JSONPath is a query language for JSON with features similar to XPath for XML. JSONPath is used for selecting and extracting a sub-section from the JSON document.
XQuery is XPath compliant. It uses XPath expressions to restrict the search results on XML collections.
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
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.
// Select a single item
people[1].firstName
// Select a slice of an array
people[0:5]
// Select all the first names
people[*].firstName
// Select all first names based on search term
people[?state=='VA'].firstName
// Count how many people are over 35
length(people[?age>`35`])
// Select only the name and age of people over 35
people[?age>`35`].{name: name, age: age}
// Join expressions together to sort and join elements into a string
people[?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.
There are also some other options for traversing/filtering JSON data, along with some syntax examples to help you 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)
$.automobiles[?(@.maker='Honda')].model
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.
Other alternatives I am aware of are
HTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With