XQuery Expressions for JSON
JSON documents can be queried by entering an XPath/XQuery 3.1 query expression in the XPath/XQuery output window (see screenshot below).
To evaluate an expression on a JSON document, do the following:
1.Select either the XPath 3.1 icon or XQuery 3.1 icon.
2.Ensure that the window is in JSON evaluation mode.
3.Enter the XPath 3.1 or XQuery 3.1 expression.
4.Click Start Evaluation (at left in the toolbar).
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.
Results pane for JSON evaluation
The Results show the selected JSON components in bold in the left side of the Results pane, and the component's value in the right side of the pane. In the screenshot below, the results are displayed in bold. The array has been expanded to show its members.
Maps and arrays are displayed in short or verbose format according to whether the Show complete result icon in the toolbar is toggled off or on. Maps and arrays in the left side of the pane can be expanded by clicking their respective plus icons.