Is there a JSON equivalent of XQuery/XPath?

JavascriptJsonXpathXquery

Javascript Problem Overview


When searching for items in complex JSON arrays and hashes, like:

[
    { "id": 1, "name": "One", "objects": [
        { "id": 1, "name": "Response 1", "objects": [
            // etc.
        }]
    }
]

Is there some kind of query language I can used to find an item in [0].objects where id = 3?

Javascript Solutions


Solution 1 - Javascript

Yup, it's called JSONPath:

It's also integrated into DOJO.

Solution 2 - Javascript

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

Syntax Examples:
// 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.

Other Options

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

Solution 3 - Javascript

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.

Solution 4 - Javascript

Other alternatives I am aware of are

  1. JSONiq specification, which specifies two subtypes of languages: one that hides XML details and provides JS-like syntax, and one that enriches XQuery syntax with JSON constructors and such. Zorba implements JSONiq.
  2. Corona, which builds on top of MarkLogic provides a REST interface for storing, managing, and searching XML, JSON, Text and Binary content.
  3. MarkLogic 6 and later provide a similar REST interface as Corona out of the box.
  4. MarkLogic 8 and later support JSON natively in both their XQuery and Server-side JavaScript environment. You can apply XPath on it.

HTH.

Solution 5 - Javascript

Try to using [JSPath][1]

JSPath is a domain-specific language (DSL) that enables you to navigate and find data within your JSON documents. Using JSPath, you can select items of JSON in order to retrieve the data they contain.

JSPath for JSON like an XPath for XML.

It is heavily optimized both for Node.js and modern browsers.

[1]: https://github.com/dfilatov/jspath "JSPath"

Solution 6 - Javascript

> Is there some kind of query language ...

jq defines a JSON query language that is very similar to JSONPath -- see https://github.com/stedolan/jq/wiki/For-JSONPath-users

> ... [which] I can used to find an item in [0].objects where id = 3?

I'll assume this means: find all JSON objects under the specified key with id == 3, no matter where the object may be. A corresponding jq query would be:

.[0].objects | .. | objects | select(.id==3)

where "|" is the pipe-operator (as in command shell pipes), and where the segment ".. | objects" corresponds to "no matter where the object may be".

The basics of jq are largely obvious or intuitive or at least quite simple, and most of the rest is easy to pick up if you're at all familiar with command-shell pipes. The jq FAQ has pointers to tutorials and the like.

jq is also like SQL in that it supports CRUD operations, though the jq processor never overwrites its input. jq can also handle streams of JSON entities.

Two other criteria you might wish to consider in assessing a JSON-oriented query language are:

  • does it support regular expressions? (jq 1.5 has comprehensive support for PCRE regex)

  • is it Turing-complete? (yep)

Solution 7 - Javascript

XQuery can be used to query JSON, provided that the processor offers JSON support. This is a straightforward example how BaseX can be used to find objects with "id" = 1:

json:parse('[
    { "id": 1, "name": "One", "objects": [
        { "id": 1, "name": "Response 1", "objects": [ "etc." ] }
    ]}
]')//value[.//id = 1]

Solution 8 - Javascript

Json Pointer seem's to be getting growing support too.

Solution 9 - Javascript

Defiant.js looks also pretty cool, here's a simple example:

var obj = {
        "car": [
            {"id": 10, "color": "silver", "name": "Volvo"},
            {"id": 11, "color": "red",    "name": "Saab"},
            {"id": 12, "color": "red",    "name": "Peugeot"},
            {"id": 13, "color": "yellow", "name": "Porsche"}
        ],
        "bike": [
            {"id": 20, "color": "black", "name": "Cannondale"},
            {"id": 21, "color": "red",   "name": "Shimano"}
        ]
    },
    search = JSON.search(obj, '//car[color="yellow"]/name');

console.log( search );
// ["Porsche"]

var reds = JSON.search(obj, '//*[color="red"]');

for (var i=0; i<reds.length; i++) {
    console.log( reds[i].name );
}
// Saab
// Peugeot
// Shimano

Solution 10 - Javascript

Jsel is awesome and is based on a real XPath engine. It allows you to create XPath expressions to find any type of JavaScript data, not just objects (strings too).

You can create custom schemas and mappings to give you complete control over how your data is walkable by the XPath engine. A schema is a way of defining how elements, children, attributes, and node values are defined in your data. Then you can create your own expressions to suit.

Given you had a variable called data which contained the JSON from the question, you could use jsel to write:

jsel(data).select("//*[@id=3]")

This will return any node with an id attribute of 3. An attribute is any primitive (string, number, date, regex) value within an object.

Solution 11 - Javascript

ObjectPath is a query language similar to XPath or JSONPath, but much more powerful thanks to embedded arithmetic calculations, comparison mechanisms and built-in functions. See the syntax:

Find in the shop all shoes of red color and price less than 50

$..shoes.*[color is "red" and price < 50]

Solution 12 - Javascript

JMESPath seems to be very popular these days (as of 2020) and addresses a number of issues with JSONPath. It's available for many languages.

Solution 13 - Javascript

@Naftule - with "defiant.js", it is possible to query a JSON structure with XPath expressions. Check out this evaluator to get an idea of how it works:

http://www.defiantjs.com/#xpath_evaluator

Unlike JSONPath, "defiant.js" delivers the full-scale support of the query syntax - of XPath on JSON structures.

The source code of defiant.js can be found here:
https://github.com/hbi99/defiant.js

Solution 14 - Javascript

Just to add to the choices, there's also XPath. XPath 3.1 handles JSON as well as XML. In XPath 3.1 your required query is ?0?Objects?*[?id=3]

Solution 15 - Javascript

The latest XPath spec includes JSON support:

> The primary purpose of XPath is to address the nodes of XML trees and JSON trees. XPath gets its name from its use of a path notation for navigating through the hierarchical structure of an XML document. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath 3.1 adds a similar syntax for navigating JSON trees.

This is also the case for XQuery:

> JSON is a lightweight data-interchange format that is widely used to exchange data on the web and to store data in databases. Many applications use JSON together with XML and HTML. XQuery 3.1 extends XQuery to support JSON as well as XML, adding maps and arrays to the data model and supporting them with new expressions in the language and new functions in [XQuery and XPath Functions and Operators 3.1].

Solution 16 - Javascript

If you're like me and you just want to do path-based lookups, but don't care about real XPath, lodash's _.get() can work. Example from lodash docs:

var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c');
// → 3

_.get(object, ['a', '0', 'b', 'c']);
// → 3

_.get(object, 'a.b.c', 'default');
// → 'default'

Solution 17 - Javascript

Try this out - https://github.com/satyapaul/jpath/blob/master/JSONDataReader.java

It's a very simple implementation on similar line of xpath for xml. It's names as jpath.

Solution 18 - Javascript

I know the OP tagged the question with javascript but in my case I was looking exactly the same thing but from a Java backend (with Camel).

An interesting thing that can also be useful if you are using an integration framework like Camel, jsonPath is also supported by a specific Camel Component since Camel 2.13.

Example from the Camel doc above:

from("queue:books.new")
  .choice()
    .when().jsonpath("$.store.book[?(@.price < 10)]")
      .to("jms:queue:book.cheap")
    .when().jsonpath("$.store.book[?(@.price < 30)]")
      .to("jms:queue:book.average")
    .otherwise()
      .to("jms:queue:book.expensive")

which is quite straightforward to use.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNaftuli KayView Question on Stackoverflow
Solution 1 - JavascriptMike ChristensenView Answer on Stackoverflow
Solution 2 - JavascriptSimon EastView Answer on Stackoverflow
Solution 3 - JavascriptBrian ClozelView Answer on Stackoverflow
Solution 4 - JavascriptgrtjnView Answer on Stackoverflow
Solution 5 - JavascriptdfilatovView Answer on Stackoverflow
Solution 6 - JavascriptpeakView Answer on Stackoverflow
Solution 7 - JavascriptChristian GrünView Answer on Stackoverflow
Solution 8 - JavascriptkarlfreemanView Answer on Stackoverflow
Solution 9 - JavascriptEpocView Answer on Stackoverflow
Solution 10 - JavascriptAliView Answer on Stackoverflow
Solution 11 - JavascriptEla BednarekView Answer on Stackoverflow
Solution 12 - JavascriptjlhView Answer on Stackoverflow
Solution 13 - JavascriptHakan BilginView Answer on Stackoverflow
Solution 14 - JavascriptMichael KayView Answer on Stackoverflow
Solution 15 - Javascriptleo60228View Answer on Stackoverflow
Solution 16 - JavascriptadittesView Answer on Stackoverflow
Solution 17 - JavascriptSatyajit PaulView Answer on Stackoverflow
Solution 18 - JavascriptрüффпView Answer on Stackoverflow