Is there a JSON equivalent of XQuery/XPath?

Asked
Active3 hr before
Viewed126 times

6 Answers

90%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Unlike JSONPath, "defiant.js" delivers the full-scale support of the query syntax - of XPath on JSON structures., Stack Overflow help chat ,@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:

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(', ', @)
load more v
88%

If we’re manipulating XML, we can use the XQuery or XPath query languages to find the elements we want in the XML code.,In JSON, there’s no such query language built-in natively in any runtime environment, but there’re many libraries that let us do the same thing.,In this article, we’ll look at the equivalents of XQuery or XPath with JSON.,We can use the JSPath and JSONPath libraries to query for items in JavaScript objects.

To use it, we can run:

npm i jspath
load more v
72%

When searching for items in complex JSON arrays and hashes, like:,JSONselect has another point of view on the question (CSS selector-like, rather than XPath) and has a JavaScript implementation.,I think JSONQuery is a superset of JSONPath and thus replaces it in dojo. Then there's also RQL.,Two other criteria you might wish to consider in assessing a JSON-oriented query language are:

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

[{
      "id": 1,
      "name": "One",
      "objects": [{
            "id": 1,
            "name": "Response 1",
            "objects": [
               // etc.
            }]
      }
   ]
load more v
65%

If we’re manipulating XML, we can use the XQuery or XPath query languages to find the elements we want in the XML code.,In JSON, there’s no such query language built-in natively in any runtime environment, but there’re many libraries that let us do the same thing.,In this article, we’ll look at the equivalents of XQuery or XPath with JSON.,One library that we can use to query JSON objects is the JSPath library.

To use it, we can run:

npm i jspath
load more v
75%

For more details, see XPath Quick Reference in the XQuery and XSLT Reference Guide.,For more details, see the JavaScript Reference Guide.,A complex XML node has a string value for indexing purposes that is the concatenation of the text nodes of all its descendant nodes. There is no equivalent string value for a JSON object node. ,For the signatures and description of each function, see the MarkLogic XQuery and XSLT Function Reference.

The name of a node is the name of the innermost JSON property name. For example, in the node tree above, "property2" is the name of both the array node and each of the array member nodes.

fn: node - name(fn: doc($uri) / property2 / array - node()) == > "property2"
fn: node - name(fn: doc($uri) / property2[1]) == > "property2"

Nodes which do not have an enclosing property are unnamed nodes. For example, the following array node has no name, so neither do its members. Therefore, when you try to get the name of the node in XQuery using fn:node-name, an empty sequence is returned.

let $node: = array - node {
   1,
   2
}
return fn: node - name($node //number-node[. eq 1])
      ==
      > an empty sequence
load more v
40%

all codepoints that do not represent characters that are valid in the version of XML supported by the processor, including codepoints representing unpaired surrogates;,The possibility of the input containing characters that are not valid in XML (for example, unpaired surrogates) arises only when such characters are expressed using JSON escape sequences. The is because the input to the function is an instance of xs:string, which by definition can only contain characters that are valid in XML.,An error is raised if the value of the validate option is true and the processor does not support schema validation or typed data.,The XDM tree returned by the function does not contain any unnecessary (albeit valid) nodes such as whitespace text nodes, comments, or processing instructions. It does not include any whitespace in the value of number or boolean element nodes, or in the value of escaped or escaped-key attribute nodes.

fn: json - to - xml
load more v

Other "undefined-undefined" queries related to "Is there a JSON equivalent of XQuery/XPath?"