Altova XMLSpy 2025 Enterprise Edition

XQuery Expressions for JSON

Home Prev Top Next

XQuery 3.1 expressions for JSON

Since JSON data structures commonly use objects and arrays, it is the XQuery 3.1 lookup operator ? that is used to locate nodes inside JSON objects (which are essentially maps from an XQuery perspective) and JSON arrays. This way of locating a node is different than how path expressions are written to locate nodes in XML documents. In these, the slash operator / is used to connect steps in a path expression (for example: items/*). In XQuery expressions for JSON, the slash operator is not used for locating nodes.

 

Examples of XQuery expressions for JSON

 

?items?*

Read this to mean: Lookup the child node items and then lookup all its children nodes. Note that items is expected to be a child node of the context node.

 

?Artists?1?Albums?2?Name

Read this to mean: Lookup the child node Artists and then lookup its first child node. Inside that node, lookup the child node Albums and then lookup its second child node. Now return the Name node of that second child node.

 

?Tracks?*[contains(?Writer, 'Brian')]

Read this to mean: Lookup the child node Tracks and then lookup all its children. While looking up the children, lookup each child's Writer node children, and select only those that contain the string 'Brian'. Notice that there are three lookup operators in this expression. Each is used in a new step, where a nodeset must be looked up.

 

?Artists?*[?Name="Queen"]?Albums?*?Name

Read this to mean: Inside the root object, lookup the child node Artists and then lookup all its children that have Name node with a value of "Queen". Inside these nodes, lookup all the child Albums nodes, and then their children. Inside these children, lookup (and return) the respective Name nodes. In the screenshot below, this expression is shown in the XPath/XQuery Window together with the JSON Grid View representation of the target JSON document.

 

inc-XQueryExpForJSON

 

© 2018-2024 Altova GmbH