Edit online

Querying JSON Documents with XPath or XQuery

Oxygen XML Developer Eclipse plugin provides a dedicated XPath/XQuery Builder view that allows you to compose complex XPath or XQuery expressions and execute them over JSON documents.

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 1. 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 Developer Eclipse plugin 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.