JSON Functions

In XPath 3.1, JSON data is represented using maps and arrays, and is bridged to and from XML using parse-json(), serialize(), json-to-xml(), and xml-to-json(). Native JSON support — working with JSON documents directly, without the XML round-trip shown below — is one of the major additions planned for XPath 4.0, which is under active development by the W3C's QT4CG (XQuery and XSLT 4.0 Community Group) as Community Group drafts and is not yet a W3C Recommendation.

The examples below are evaluated against:
let $m := map{"x":[1,2],"y":{"z":[10]}}
return …
map:get(parse-json(serialize($m, {"method":"json","indent":true()})),"x")[1, 2]
xml-to-json(json-to-xml(serialize($m,{"method":"json"}))){"x":[1,2],"y":{"z":[10]}}

json-doc($json_file) parses the JSON document referenced by the URI $json_file — analogous to doc() for an XML document — and returns the corresponding map or array.

ch03-json-functions.xpath.txt:
let $m := map{"x":[1,2],"y":{"z":[10]}}
return xml-to-json(json-to-xml(serialize($m,{"method":"json"})))

{"x":[1,2],"y":{"z":[10]}}