JSON Functions
These functions handle JSON parsing and conversion. The json-to-xml and xml-to-json functions convert between JSON strings and the W3C XML representation of JSON (namespace http://www.w3.org/2005/xpath-functions). The parse-json and json-doc functions convert JSON into native XPath maps, arrays, and atomic values.
parse-json
parse-json($json as xs:string) as item()?
parse-json($json as xs:string, $options as map(*)) as item()?
Parses a JSON string and returns the corresponding XPath data structure: JSON objects become XPath maps, JSON arrays become XPath arrays, strings/numbers/booleans become atomic values, and null becomes the empty sequence.
parse-json('{"name": "Alice", "age": 30}')
(: returns map {"name": "Alice", "age": 30} :)
parse-json('[1, 2, 3]')
(: returns array [1, 2, 3] :)json-doc
json-doc($href as xs:string) as item()?
json-doc($href as xs:string, $options as map(*)) as item()?
Reads a JSON file from the given URI and returns the parsed result as XPath maps, arrays, and atomic values (same conversion as parse-json).
json-doc('data.json')json-to-xml
json-to-xml($json as xs:string) as document-node()
json-to-xml($json as xs:string, $options as map(*)) as document-node()
Parses a JSON string and returns an XML document using the W3C JSON XML representation. This is useful when you want to process JSON data using XML tools such as XSLT templates and XPath axes.
JSON to XML mapping
| JSON | XML element | Example |
|---|---|---|
| Object | <map> |
{"a": 1} → <map><number key="a">1</number></map> |
| Array | <array> |
[1, 2] → <array><number>1</number><number>2</number></array> |
| String | <string> |
"hello" → <string>hello</string> |
| Number | <number> |
42 → <number>42</number> |
| Boolean | <boolean> |
true → <boolean>true</boolean> |
| Null | <null/> |
null → <null/> |
Object keys are represented as key attributes: {"name": "Alice"} becomes <string key="name">Alice</string>.
Options
The optional $options map supports:
| Key | Type | Description |
|---|---|---|
escape |
xs:boolean |
When true, preserve JSON escape sequences and mark strings with escaped="true" |
Example
<xsl:variable name="data" select="json-to-xml('{ "name": "Alice", "age": 30 }')"/>
<!-- Result:
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="name">Alice</string>
<number key="age">30</number>
</map>
-->xml-to-json
xml-to-json($input as node()) as xs:string
xml-to-json($input as node(), $options as map(*)) as xs:string
Converts an XML document in the W3C JSON XML representation back to a JSON string. This is the inverse of json-to-xml. The input must use the elements described in the mapping table above (in namespace http://www.w3.org/2005/xpath-functions).
Example
<xsl:variable name="json-string" select="xml-to-json($xml-data)"/>
<!-- If $xml-data contains:
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="name">Alice</string>
<number key="age">30</number>
</map>
Result: '{"name":"Alice","age":30}'
-->Round-trip example
A common pattern is parsing JSON, transforming the XML, and serializing back to JSON:
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:j="http://www.w3.org/2005/xpath-functions"
expand-text="yes">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template name="main">
<xsl:variable name="input" expand-text="no">{"items": [1, 2, 3]}</xsl:variable>
<xsl:variable name="xml" select="json-to-xml($input)"/>
<xsl:variable name="transformed">
<xsl:apply-templates select="$xml"/>
</xsl:variable>
<out><xsl:value-of select="xml-to-json($transformed)"/></out>
</xsl:template>
<!-- Add a "count" field to the top-level map -->
<xsl:template match="j:map[not(@key)]">
<xsl:copy>
<xsl:apply-templates/>
<j:number key="count">{count(j:array[@key='items']/*)}</j:number>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>