Edit online

Querying JSON Documents with XPath or XQuery

Oxygen XML Editor provides an XPath toolbar that makes it easy to quickly query JSON documents using XPath expressions. You can also use the dedicated XPath/XQuery Builder view that allows you to compose more complex XPath or XQuery expressions and execute them over JSON documents in Text or Author mode.

XPath Toolbar

When an XPath expression is run over a JSON document, the document is converted to XML and the XPath is executed over the converted XML document. For more information about this toolbar, see XPath Toolbar.

Figure 1. XPath Toolbar for JSON

XPath/XQuery Builder View

You can also use the XPath/XQuery view to run XPath and XQuery expressions over a JSON document. For XQuery, you need to reference the JSON document in your XQuery content. For more information about this view, see XPath Builder View.

Figure 2. XPath/XQuery Builder View for JSON

Details About Querying JSON Documents Using XPath Expressions

To execute XPath expressions over a JSON document, the document is converted to XML and the XPath is executed over the converted XML document. For this conversion, Oxygen XML Editor uses the built-in JSON to XML Converter tool. The results are mapped back to the original JSON document.

For example, if you have the following JSON document:
{
   "personnel": {
    "person": [
      {"name": "Boss"},
      {"name": "Worker"}
    ]
  },
  "id":"personnel-id"
}
and you want to match the name of the second person, the XPath expression would look like this:
/JSON/personnel/person[2]/name

The reason why the first element is JSON is because if the JSON document contains more than one property on the first level, the converted XML document will have an additional root element called <JSON>. For more information, see JSON to XML Conversion Details.

The [2] in the expression represents the index of the person in the array and in this case, it matches the second person because the index counting starts with 1.