ChatGPT解决这个技术问题 Extra ChatGPT

Is there a JSON equivalent of XQuery/XPath?

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?

not unless you make one. Leave the querying to the server, and use REST to get only the data you need.
+1 good idea. Gonna write this tomorrow…
Not XPath, but I've found JLinq pretty good (which makes code to read like in(...).where(...).select(...)): hugoware.net/Projects/jLinq.
This is frustrating because there's lots of libraries out there, but nothing approaching a commonly accepted standard. We have a library used by 3rd parties so we need to provide a query language that is widely known and used.
Sure, you can use jsel - github.com/dragonworx/jsel - given you have a variable data which contains your JSON object, you would write: jsel(data).select("//*[@id=3]") and it would return the object containing the id key with 3.

M
Mike Christensen

Yup, it's called JSONPath:

It's also integrated into DOJO.


Brian's answer suggests that the jsonQuery module should be used instead of the jsonPath module in dojo.
How solid is this? And I can't find a Java or C# version which is a deal killer for us.
The site linked here provides for Javascript and PHP. If you need a Java implementation, there’s one here: code.google.com/p/json-path
I should mention that JSONPath is not based on the XPath formal semantic. JSONiq might be a better option.
@Paramaeleon That works great. The project has been migrated to GitHub, by the way. Mike may want to add this to the answer, since people keep commenting about this.
S
Simon East

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...

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


Great summary and examples, also because of mentioning the inspiration found in either CSS selectors or XPath.
l
lorey

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.


The github JSONQuery link seems to be dead. JSONSelect also has a JavaScript version now.
g
grtjn

Other alternatives I am aware of are

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. Corona, which builds on top of MarkLogic provides a REST interface for storing, managing, and searching XML, JSON, Text and Binary content. MarkLogic 6 and later provide a similar REST interface as Corona out of the box. MarkLogic 8 and later support JSON natively in both their XQuery and Server-side JavaScript environment. You can apply XPath on it.

HTH.


There is now a JSONiq implementation: Zorba 2.6 officially supports it.
Note: MarkLogic stores JSON natively as of version 8, and allows applying XPath on it directly.
d
dfilatov

Try to using JSPath

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.


p
peak

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)


C
Christian Grün

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]

(6 years on) Saxon will run XQuery 3.1, which queries JSON. My Saxon experience is using the jar file run by java. There is a node module named saxon-java but I am not sure how that works w/json. And there is another new thing from Saxonica called Saxon-JS.
C
Community

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


E
Epoc

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

Unfortunately, not published on npm at the moment and requires manual installation...
A
Ali

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.


d
dimo414

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]


I like the first example on the website and it's great that ObjectPath can be run in an interactve, shell-like mode, but what I'm looking for is to use ObjectPath in a Python script. Can you point me to an example showing how to use ObjectPath as a library? I can't find anything like that on the website.
Please see the section about Python usage on github. We'll add this to the website - it's indeed hard to find at the moment. If you need any further help, you can post a question on google group.
Thank you, Ela, the examples added on the github page are exactly what was needed.
j
jlh

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.


Yeah that looks quite clear and clean, with a syntax very close to standard JavaScript AND is standardised across multiple languages. 👍
H
Hakan Bilgin

@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


M
Michael Kay

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]


l
leo60228

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].


a
adittes

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'

Unfortunately that function can only return a single result, it doesn't support fetching an array of matching items, which is where the other libraries shine.
S
Satyajit Paul

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.


This question is tagged javascript but this library appears to be for java
It has a Javascript version too - github.com/satyapaul/jpath/blob/master/jpath.js Here is he git home page for the project - github.com/satyapaul/jpath
р
рüффп

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.