Difference between revisions of "JSON Module"

From BaseX Documentation
Jump to navigation Jump to search
m (Text replace - "\[\[Category:XQuery\]\]" to "")
 
(27 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This [[Module Library|XQuery Module]] contains functions to parse and serialize JSON documents. [http://www.json.org/ JSON (JavaScript Object Notation)] is a popular data exchange format for applications written in JavaScript. As there are notable differences between JSON and XML, no mapping exists that guarantees a lossless, bidirectional conversion between JSON and XML. For this reason, we offer various mappings, all of which are suited to different use cases.
+
This [[Module Library|XQuery Module]] contains functions to parse and serialize JSON data [https://www.json.org/ JSON (JavaScript Object Notation)] is a popular data exchange format for applications written in JavaScript. As there are notable differences between JSON and XML, or XQuery data types, no mapping exists that guarantees a lossless, bidirectional conversion between JSON and XML. For this reason, we offer various mappings, all of which are suited to different use cases.
  
 
=Conventions=
 
=Conventions=
  
All functions in this module are assigned to the <code><nowiki>http://basex.org/modules/json</nowiki></code> namespace, which is statically bound to the {{Code|json}} prefix.<br/>
+
All functions and errors in this module are assigned to the <code><nowiki>http://basex.org/modules/json</nowiki></code> namespace, which is statically bound to the {{Code|json}} prefix.<br/>
All errors are assigned to the <code><nowiki>http://basex.org/errors</nowiki></code> namespace, which is statically bound to the {{Code|bxerr}} prefix.
 
  
 
==Conversion Formats==
 
==Conversion Formats==
 +
 +
'''A little advice''': in the Database Creation dialog of the GUI, if you select JSON Parsing and switch to the ''Parsing'' tab, you can see the effects of some of the conversion options.
  
 
===Direct===
 
===Direct===
Line 12: Line 13:
 
The {{Code|direct}} conversion format allows a lossless conversion from JSON to XML and back. The transformation is based on the following rules:
 
The {{Code|direct}} conversion format allows a lossless conversion from JSON to XML and back. The transformation is based on the following rules:
  
* The resulting document has a {{Code|<json>}} root node.  
+
* The resulting document has a {{Code|json}} root node.  
* Object pairs are represented via elements. The name of a pair is rewritten to an element name:
+
* Object pairs are represented via elements. The name of a pair is encoded, as described in the [[Conversion Module#Keys|Conversion Module]], and used as element name.
** Empty names are represented by a single underscore ({{Code|_}}). Existing underscores are rewritten to two underscores ({{Code|__}}), and characters that are not valid in element names are rewritten to an underscore and the character’s four-digit Unicode.
+
* Array entries are also represented via elements, with {{Code|_}} as element name.
** If the {{Code|lax}} option is set to {{Code|true}}, invalid characters are simply replaced with underscores or (when invalid as first character of an element name) prefixed with an underscore. The resulting names are better readable, but cannot always be converted back to their original form.
 
* Array entries are also represented via elements. {{Code|_}} is used as element name.
 
 
* Object and array values are stored in text nodes.
 
* Object and array values are stored in text nodes.
 
* The types of values are represented via {{Code|type}} attributes:
 
* The types of values are represented via {{Code|type}} attributes:
Line 26: Line 25:
 
The {{Code|attributes}} format is lossless, too. The transformation based on the following rules:
 
The {{Code|attributes}} format is lossless, too. The transformation based on the following rules:
  
* The resulting document has a {{Code|<json>}} root node.  
+
* The resulting document has a {{Code|json}} root node.  
* Object pairs are represented via {{Code|<pair>}} elements. The name of a pair is stored in a {{Code|name}} attribute.
+
* Object pairs are represented via {{Code|pair}} elements. The name of a pair is stored in a {{Code|name}} attribute.
* Array entries are represented via {{Code|<item>}} elements.
+
* Array entries are represented via {{Code|item}} elements.
 
* Object and array values are stored in text nodes.
 
* Object and array values are stored in text nodes.
 
* The types of values are represented via {{Code|type}} attributes:
 
* The types of values are represented via {{Code|type}} attributes:
 
** The existing types are ''string'', ''number'', ''boolean'', ''null'', ''object'', and ''array''.
 
** The existing types are ''string'', ''number'', ''boolean'', ''null'', ''object'', and ''array''.
 
** As most values are strings, the ''string'' type is by default omitted.
 
** As most values are strings, the ''string'' type is by default omitted.
 
===Map===
 
 
The {{Code|map}} format is lossless, too. It converts a JSON document to an XQuery map and vice versa. The conversion rules are the same as for [[XQuery 3.1#fn:parse-json|fn:parse-json]].
 
  
 
===Basic===
 
===Basic===
Line 46: Line 41:
 
The {{Code|jsonml}} format is designed to convert XML to JSON and back, using the JsonML dialect. JsonML allows the transformation of arbitrary XML documents, but namespaces, comments and processing instructions will be discarded in the transformation process. More details are found in the official [http://jsonml.org/XML JsonML documentation].
 
The {{Code|jsonml}} format is designed to convert XML to JSON and back, using the JsonML dialect. JsonML allows the transformation of arbitrary XML documents, but namespaces, comments and processing instructions will be discarded in the transformation process. More details are found in the official [http://jsonml.org/XML JsonML documentation].
  
'''A little advice''': in the Database Creation dialog of the GUI, if you select JSON Parsing and switch to the ''Parsing'' tab, you can see the effects of some of the conversion options.
+
===XQuery===
 +
 
 +
The {{Code|xquery}} format is lossless, too. It converts JSON data to an XQuery value (a map, array, string, number, boolean, or empty sequence) and vice versa. The conversion rules are the same as for [[XQuery 3.1#fn:parse-json|fn:parse-json]].
 +
 
 +
The resulting representation consumes less memory than XML-based formats, and values can be directly accessed without conversion. Thus, it is recommendable for very large inputs and for efficient ad-hoc processing.
  
 
==Options==
 
==Options==
 
{{Mark|Updated with Version 8.4}}: <code>unescape</code> changed to <code>escape</code>.
 
  
 
The following options are available (the ''Direction'' column indicates if an option applies to parsing, serialization, or both operations):
 
The following options are available (the ''Direction'' column indicates if an option applies to parsing, serialization, or both operations):
Line 64: Line 61:
 
| {{Code|format}}
 
| {{Code|format}}
 
| Specifies the format for converting JSON data.
 
| Specifies the format for converting JSON data.
| {{Code|direct}}, {{Code|attributes}}, {{Code|jsonml}}, {{Code|map}}
+
| {{Code|direct}}, {{Code|attributes}}, {{Code|jsonml}}, {{Code|xquery}}
 
| {{Code|direct}}
 
| {{Code|direct}}
 
| ''parse'', ''serialize''
 
| ''parse'', ''serialize''
 
|- valign="top"
 
|- valign="top"
 
| {{Code|liberal}}
 
| {{Code|liberal}}
| Determines if minor deviations from [http://www.rfc-editor.org/rfc/rfc7159.txt RFC 7159] will be ignored.
+
| Determines if minor deviations from [https://www.rfc-editor.org/rfc/rfc7159.txt RFC 7159] will be ignored.
 
| {{Code|yes}}, {{Code|no}}
 
| {{Code|yes}}, {{Code|no}}
 
| {{Code|no}}
 
| {{Code|no}}
Line 76: Line 73:
 
| {{Code|merge}}
 
| {{Code|merge}}
 
| This option is considered when {{Code|direct}} or {{Code|attributes}} conversion is used:<br/>
 
| This option is considered when {{Code|direct}} or {{Code|attributes}} conversion is used:<br/>
* If a name has the same type throughout the document, the {{Code|type}} attribute will be omitted. Instead, the name will be listed in additional, type-specific attributes in the root node
+
* If a name has the same type throughout the data, the {{Code|type}} attribute will be omitted. Instead, the name will be listed in additional, type-specific attributes in the root node.
 
* The attributes are named by their type in plural (''numbers'', ''booleans'', ''nulls'', ''objects'' and ''arrays''), and the attribute value contains all names with that type, separated by whitespaces.
 
* The attributes are named by their type in plural (''numbers'', ''booleans'', ''nulls'', ''objects'' and ''arrays''), and the attribute value contains all names with that type, separated by whitespaces.
 
| {{Code|yes}}, {{Code|no}}
 
| {{Code|yes}}, {{Code|no}}
Line 120: Line 117:
 
|-
 
|-
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|json:parse|$input as xs:string|element(json)}}<br/>{{Func|json:parse|$input as xs:string, $options as map(xs:string, xs:string)|item()}}
+
|{{Func|json:parse|$string as xs:string?|item()?}}<br/>{{Func|json:parse|$string as xs:string?, $options as map(*)?|item()?}}
 +
|-
 +
| '''Summary'''
 +
|Converts the JSON {{Code|$string}} to an XQuery value. If the input can be successfully parsed, it can be serialized back to the original JSON representation. The {{Code|$options}} argument can be used to control the way the input is converted.
 +
|-
 +
| '''Errors'''
 +
|{{Error|parse|#Errors}} the specified input cannot be parsed as JSON document.<br/>{{Error|options|#Errors}} the specified options are conflicting.
 +
|}
 +
 
 +
==json:doc==
 +
 
 +
{{Mark|Introduced with BaseX 9.4:}}
 +
 
 +
{| width='100%'
 +
|-
 +
| width='120' | '''Signatures'''
 +
|{{Func|json:doc|$uri as xs:string|item()?}}<br />{{Func|json:doc|$uri as xs:string, $options as map(*)?|item()?}}<br />
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Converts the JSON document specified by {{Code|$input}} to an XML document or a map. If the input can be successfully parsed, it can be serialized back to the original JSON representation. The {{Code|$options}} argument can be used to control the way the input is converted.
+
|Fetches the JSON document referred to by the given {{Code|$uri}} and converts it to an XQuery value. The {{Code|$options}} argument can be used to control the way the input is converted.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|{{Error|BXJS0001|#Errors}} the specified input cannot be parsed as JSON document.
+
|{{Error|parse|#Errors}} the specified input cannot be parsed as JSON document.<br/>{{Error|options|#Errors}} the specified options are conflicting.
 
|}
 
|}
  
Line 134: Line 147:
 
|-
 
|-
 
| width='120' | '''Signatures'''
 
| width='120' | '''Signatures'''
|{{Func|json:serialize|$input as item()?|xs:string}}<br/>{{Func|json:serialize|$input as item()?, $options as map(xs:string, xs:string)|xs:string}}
+
|{{Func|json:serialize|$input as item()?|xs:string}}<br/>{{Func|json:serialize|$input as item()?, $options as map(*)?|xs:string}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Serializes the node specified by {{Code|$input}} as JSON, and returns the result as {{Code|xs:string}} instance. The node is expected to conform to the output created by the [[#json:parse|json:parse()]] function. All other items will be serialized as specified for the [[XQuery 3.1#JSON Serialization|json output method]] of the official specification.<br />Items can also be serialized as JSON if the [[Serialization|Serialization Parameter]] {{Code|method}} is set to {{Code|json}}.<br/>The {{Code|$options}} argument can be used to control the way the input is serialized.
+
|Serializes the specified {{Code|$input}} as JSON, using the specified {{Code|$options}}, and returns the result as string:
 +
* The input is expected to conform to the results that are created by [[#json:parse|json:parse()]].
 +
* Non-conforming items will be serialized as specified in the [[XQuery 3.1#JSON Serialization|json output method]] of the official recommendation.
 +
Values can also be serialized as JSON with the standard [[Serialization]] feature of XQuery:
 +
* The parameter {{Code|method}} needs to be set to {{Code|json}}, and
 +
* the options presented in this article need to be assigned to the {{Code|json}} parameter.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|{{Error|BXJS0002|#Errors}} the specified node cannot be serialized as JSON document.
+
|{{Error|serialize|#Errors}} the specified node cannot be serialized as JSON document.
 
|}
 
|}
  
Line 150: Line 168:
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
let $database := "database"
 
let $database := "database"
 
for $name in file:list('.', false(), '*.json')
 
for $name in file:list('.', false(), '*.json')
Line 156: Line 174:
 
let $json := json:parse($file)
 
let $json := json:parse($file)
 
return db:add($database, $json, $name)  
 
return db:add($database, $json, $name)  
</pre>
+
</syntaxhighlight>
  
 
'''Example 2: Converts a simple JSON string to XML and back'''
 
'''Example 2: Converts a simple JSON string to XML and back'''
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
json:parse('{}')
 
json:parse('{}')
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<json type="object"/>
 
<json type="object"/>
</pre>
+
</syntaxhighlight>
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
(: serialize result as plain text :)
 
(: serialize result as plain text :)
 
declare option output:method 'text';
 
declare option output:method 'text';
 
json:serialize(<json type="object"/>)
 
json:serialize(<json type="object"/>)
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
{ }
 
{ }
</pre>
+
</syntaxhighlight>
  
 
'''Example 3: Converts a JSON string with simple objects and arrays'''
 
'''Example 3: Converts a JSON string with simple objects and arrays'''
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
json:parse('{
 
json:parse('{
 
   "title": "Talk On Travel Pool",
 
   "title": "Talk On Travel Pool",
Line 193: Line 211:
 
   "generator": "http://www.flickr.com/"
 
   "generator": "http://www.flickr.com/"
 
}')
 
}')
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<json type="object">
 
<json type="object">
 
   <title>Talk On Travel Pool</title>
 
   <title>Talk On Travel Pool</title>
Line 204: Line 222:
 
   <generator>http://www.flickr.com/</generator>
 
   <generator>http://www.flickr.com/</generator>
 
</json>
 
</json>
</pre>
+
</syntaxhighlight>
  
 
'''Example 4: Converts a JSON string with different data types'''
 
'''Example 4: Converts a JSON string with different data types'''
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
let $options := map { 'merge': true() }
 
let $options := map { 'merge': true() }
 
return json:parse('{
 
return json:parse('{
Line 231: Line 249:
 
   ]
 
   ]
 
}', $options)
 
}', $options)
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<json numbers="age code" arrays="phone" objects="json address value">
 
<json numbers="age code" arrays="phone" objects="json address value">
 
   <first__name>John</first__name>
 
   <first__name>John</first__name>
Line 255: Line 273:
 
   </phone>
 
   </phone>
 
</json>
 
</json>
</pre>
+
</syntaxhighlight>
  
 
==JsonML Format==
 
==JsonML Format==
  
'''Example 1: Converts all XML documents in a database to JsonML and writes them to disk'''
+
'''Example 1: Converts all XML documents in a database to the JsonML format and writes them to disk'''
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
for $doc in collection('json')
 
for $doc in collection('json')
 
let $name := document-uri($doc)
 
let $name := document-uri($doc)
let $json := json:serialize($doc)
+
let $json := json:serialize($doc, map { 'format': 'jsonml' })
 
return file:write($name, $json)
 
return file:write($name, $json)
</pre>
+
</syntaxhighlight>
  
'''Example 2: Converts a simple XML fragment to the JsonML format'''
+
'''Example 2: Converts an XML document with elements and text'''
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
json:serialize(<xml/>, map { 'format': 'jsonml' })
 
</pre>
 
 
 
'''Result:'''
 
<pre>
 
["xml"]
 
</pre>
 
 
 
'''Example 3: Converts an XML document with elements and text'''
 
 
 
'''Query:'''
 
<pre class="brush:xquery">
 
 
json:serialize(doc('flickr.xml'), map { 'format': 'jsonml' })
 
json:serialize(doc('flickr.xml'), map { 'format': 'jsonml' })
</pre>
+
</syntaxhighlight>
  
 
'''flickr.xml:'''
 
'''flickr.xml:'''
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<flickr>
 
<flickr>
 
   <title>Talk On Travel Pool</title>
 
   <title>Talk On Travel Pool</title>
Line 297: Line 303:
 
   <generator>http://www.flickr.com/</generator>
 
   <generator>http://www.flickr.com/</generator>
 
</flickr>
 
</flickr>
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre>
+
<syntaxhighlight lang="json">
 
["flickr",
 
["flickr",
 
   ["title",
 
   ["title",
Line 312: Line 318:
 
   ["generator",
 
   ["generator",
 
     "http://www.flickr.com/"]]
 
     "http://www.flickr.com/"]]
</pre>
+
</syntaxhighlight>
  
'''Example 4: Converts a document with nested elements and attributes'''
+
'''Example 3: Converts a document with nested elements and attributes to JsonML'''
  
 
'''Query:'''
 
'''Query:'''
<pre class="brush:xquery">
+
<syntaxhighlight lang="xquery">
 
json:serialize(doc('input.xml'), map { 'format': 'jsonml' })
 
json:serialize(doc('input.xml'), map { 'format': 'jsonml' })
</pre>
+
</syntaxhighlight>
  
 
'''input.xml:'''
 
'''input.xml:'''
<pre class="brush:xml">
+
<syntaxhighlight lang="xml">
 
<address id='1'>
 
<address id='1'>
 
   <!-- comments will be discarded -->
 
   <!-- comments will be discarded -->
Line 334: Line 340:
 
   <phone type='home'>212 555-1234</phone>
 
   <phone type='home'>212 555-1234</phone>
 
</address>
 
</address>
</pre>
+
</syntaxhighlight>
  
 
'''Result:'''
 
'''Result:'''
<pre>
+
<syntaxhighlight lang="json">
 
["address", {"id":"1"},
 
["address", {"id":"1"},
 
   ["last_name",
 
   ["last_name",
Line 352: Line 358:
 
   ["phone", {"type":"home"},
 
   ["phone", {"type":"home"},
 
     "212 555-1234"]]
 
     "212 555-1234"]]
</pre>
+
</syntaxhighlight>
 +
 
 +
==XQuery Format==
 +
 
 +
'''Example 1: Converts a JSON string to XQuery'''
 +
 
 +
'''Query:'''
 +
<syntaxhighlight lang="xquery">
 +
let $input := '{
 +
  "Title": "Drinks",
 +
  "Author": [ "Jim Daniels", "Jack Beam" ]
 +
}'
 +
let $data := json:parse($input, map { 'format': 'xquery' })
 +
return map:for-each($data, function($k, $v) {
 +
  $k || ': ' || string-join($v, ', ')
 +
})
 +
</syntaxhighlight>
 +
 
 +
'''Result:'''
 +
<syntaxhighlight lang="json">
 +
Author: Jim Daniels, Jack Beam
 +
Title: Drinks
 +
</syntaxhighlight>
 +
 
 +
'''Example 2: Converts XQuery data to JSON'''
 +
 
 +
'''Query:'''
 +
<syntaxhighlight lang="xquery">
 +
for $item in (
 +
  true(),
 +
  'ABC',
 +
  array { 1 to 5 },
 +
  map { "Key": "Value" }
 +
)
 +
return json:serialize(
 +
  $item,
 +
  map { 'format': 'xquery', 'indent': 'no' }
 +
)
 +
</syntaxhighlight>
 +
 
 +
'''Result:'''
 +
<syntaxhighlight lang="json">
 +
true
 +
"ABC"
 +
[1,2,3,4,5]
 +
{"Key":"Value"}
 +
</syntaxhighlight>
  
 
=Errors=
 
=Errors=
Line 360: Line 412:
 
|Description
 
|Description
 
|-
 
|-
|{{Code|BXJS0001}}
+
|{{Code|options}}
 +
|The specified options are conflicting.
 +
|-
 +
|{{Code|parse}}
 
|The specified input cannot be parsed as JSON document.
 
|The specified input cannot be parsed as JSON document.
 
|-
 
|-
|{{Code|BXJS0002}}
+
|{{Code|serialize}}
 
|The specified node cannot be serialized as JSON document.
 
|The specified node cannot be serialized as JSON document.
 
|}
 
|}
  
 
=Changelog=
 
=Changelog=
 +
 +
;Version 9.4
 +
* Added: [[#json:doc|json:doc]]
 +
 +
; Version 9.1
 +
* Updated: [[#json:parse|json:parse]] can be called with empty sequence.
 +
 +
;Version 9.0
 +
* Updated: <code>map</code> format renamed to <code>xquery</code>.
 +
* Updated: error codes updated; errors now use the module namespace
  
 
;Version 8.4
 
;Version 8.4

Latest revision as of 13:06, 2 July 2020

This XQuery Module contains functions to parse and serialize JSON data JSON (JavaScript Object Notation) is a popular data exchange format for applications written in JavaScript. As there are notable differences between JSON and XML, or XQuery data types, no mapping exists that guarantees a lossless, bidirectional conversion between JSON and XML. For this reason, we offer various mappings, all of which are suited to different use cases.

Conventions[edit]

All functions and errors in this module are assigned to the http://basex.org/modules/json namespace, which is statically bound to the json prefix.

Conversion Formats[edit]

A little advice: in the Database Creation dialog of the GUI, if you select JSON Parsing and switch to the Parsing tab, you can see the effects of some of the conversion options.

Direct[edit]

The direct conversion format allows a lossless conversion from JSON to XML and back. The transformation is based on the following rules:

  • The resulting document has a json root node.
  • Object pairs are represented via elements. The name of a pair is encoded, as described in the Conversion Module, and used as element name.
  • Array entries are also represented via elements, with _ as element name.
  • Object and array values are stored in text nodes.
  • The types of values are represented via type attributes:
    • The existing types are string, number, boolean, null, object, and array.
    • As most values are strings, the string type is by default omitted.

Attributes[edit]

The attributes format is lossless, too. The transformation based on the following rules:

  • The resulting document has a json root node.
  • Object pairs are represented via pair elements. The name of a pair is stored in a name attribute.
  • Array entries are represented via item elements.
  • Object and array values are stored in text nodes.
  • The types of values are represented via type attributes:
    • The existing types are string, number, boolean, null, object, and array.
    • As most values are strings, the string type is by default omitted.

Basic[edit]

The basic format is another lossless format. It converts a JSON document to an XML node and vice versa. The conversion rules are the same as for fn:json-to-xml.

JsonML[edit]

The jsonml format is designed to convert XML to JSON and back, using the JsonML dialect. JsonML allows the transformation of arbitrary XML documents, but namespaces, comments and processing instructions will be discarded in the transformation process. More details are found in the official JsonML documentation.

XQuery[edit]

The xquery format is lossless, too. It converts JSON data to an XQuery value (a map, array, string, number, boolean, or empty sequence) and vice versa. The conversion rules are the same as for fn:parse-json.

The resulting representation consumes less memory than XML-based formats, and values can be directly accessed without conversion. Thus, it is recommendable for very large inputs and for efficient ad-hoc processing.

Options[edit]

The following options are available (the Direction column indicates if an option applies to parsing, serialization, or both operations):

Option Description Allowed Default Direction
format Specifies the format for converting JSON data. direct, attributes, jsonml, xquery direct parse, serialize
liberal Determines if minor deviations from RFC 7159 will be ignored. yes, no no parse
merge This option is considered when direct or attributes conversion is used:
  • If a name has the same type throughout the data, the type attribute will be omitted. Instead, the name will be listed in additional, type-specific attributes in the root node.
  • The attributes are named by their type in plural (numbers, booleans, nulls, objects and arrays), and the attribute value contains all names with that type, separated by whitespaces.
yes, no no parse, serialize
strings Indicates if type attributes will be added for strings. yes, no yes parse, serialize
lax Specifies if a lax approach is used to convert QNames to JSON names. yes, no no parse, serialize
escape Indicates if escaped characters are expanded (for example, \n becomes a single x0A character, while \u20AC becomes the character ). yes, no yes parse
escape Indicates if characters are escaped whenever the JSON syntax requires it. This option can be set to no if strings are already in escaped form and no further escaping is permitted. yes, no yes serialize
indent Indicates if whitespace should be added to the output with the aim of improving human legibility. If the parameter is set as in the query prolog, it overrides the indent serialization parameter. yes, no yes serialize

Functions[edit]

json:parse[edit]

Signatures json:parse($string as xs:string?) as item()?
json:parse($string as xs:string?, $options as map(*)?) as item()?
Summary Converts the JSON $string to an XQuery value. If the input can be successfully parsed, it can be serialized back to the original JSON representation. The $options argument can be used to control the way the input is converted.
Errors parse: the specified input cannot be parsed as JSON document.
options: the specified options are conflicting.

json:doc[edit]

Introduced with BaseX 9.4:

Signatures json:doc($uri as xs:string) as item()?
json:doc($uri as xs:string, $options as map(*)?) as item()?
Summary Fetches the JSON document referred to by the given $uri and converts it to an XQuery value. The $options argument can be used to control the way the input is converted.
Errors parse: the specified input cannot be parsed as JSON document.
options: the specified options are conflicting.

json:serialize[edit]

Signatures json:serialize($input as item()?) as xs:string
json:serialize($input as item()?, $options as map(*)?) as xs:string
Summary Serializes the specified $input as JSON, using the specified $options, and returns the result as string:
  • The input is expected to conform to the results that are created by json:parse().
  • Non-conforming items will be serialized as specified in the json output method of the official recommendation.

Values can also be serialized as JSON with the standard Serialization feature of XQuery:

  • The parameter method needs to be set to json, and
  • the options presented in this article need to be assigned to the json parameter.
Errors serialize: the specified node cannot be serialized as JSON document.

Examples[edit]

BaseX Format[edit]

Example 1: Adds all JSON documents in a directory to a database

Query:

let $database := "database"
for $name in file:list('.', false(), '*.json')
let $file := file:read-text($name)
let $json := json:parse($file)
return db:add($database, $json, $name)

Example 2: Converts a simple JSON string to XML and back

Query:

json:parse('{}')

Result:

<json type="object"/>

Query:

(: serialize result as plain text :)
declare option output:method 'text';
json:serialize(<json type="object"/>)

Result:

{ }

Example 3: Converts a JSON string with simple objects and arrays

Query:

json:parse('{
  "title": "Talk On Travel Pool",
  "link": "http://www.flickr.com/groups/talkontravel/pool/",
  "description": "Travel and vacation photos from around the world.",
  "modified": "2014-02-02T11:10:27Z",
  "generator": "http://www.flickr.com/"
}')

Result:

<json type="object">
  <title>Talk On Travel Pool</title>
  <link>http://www.flickr.com/groups/talkontravel/pool/</link>
  <description>Travel and vacation photos from around the world.</description>
  <modified>2014-02-02T11:10:27Z</modified>
  <generator>http://www.flickr.com/</generator>
</json>

Example 4: Converts a JSON string with different data types

Query:

let $options := map { 'merge': true() }
return json:parse('{
  "first_name": "John",
  "last_name": "Smith",
  "age": 25,
  "address": {
    "street": "21 2nd Street",
    "city": "New York",
    "code": 10021
  },
  "phone": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "mobile",
      "number": 1327724623
    }
  ]
}', $options)

Result:

<json numbers="age code" arrays="phone" objects="json address value">
  <first__name>John</first__name>
  <last__name>Smith</last__name>
  <age>25</age>
  <address>
    <street>21 2nd Street</street>
    <city>New York</city>
    <code>10021</code>
  </address>
  <phone>
    <_>
      <type>home</type>
      <number>212 555-1234</number>
    </_>
    <_>
      <type>mobile</type>
      <number type="number">1327724623</number>
    </_>
  </phone>
</json>

JsonML Format[edit]

Example 1: Converts all XML documents in a database to the JsonML format and writes them to disk

Query:

for $doc in collection('json')
let $name := document-uri($doc)
let $json := json:serialize($doc, map { 'format': 'jsonml' })
return file:write($name, $json)

Example 2: Converts an XML document with elements and text

Query:

json:serialize(doc('flickr.xml'), map { 'format': 'jsonml' })

flickr.xml:

<flickr>
  <title>Talk On Travel Pool</title>
  <link>http://www.flickr.com/groups/talkontravel/pool/</link>
  <description>Travel and vacation photos from around the world.</description>
  <modified>2014-02-02T11:10:27Z</modified>
  <generator>http://www.flickr.com/</generator>
</flickr>

Result:

["flickr",
  ["title",
    "Talk On Travel Pool"],
  ["link",
    "http://www.flickr.com/groups/talkontravel/pool/"],
  ["description",
    "Travel and vacation photos from around the world."],
  ["modified",
    "2014-02-02T11:10:27Z"],
  ["generator",
    "http://www.flickr.com/"]]

Example 3: Converts a document with nested elements and attributes to JsonML

Query:

json:serialize(doc('input.xml'), map { 'format': 'jsonml' })

input.xml:

<address id='1'>
  <!-- comments will be discarded -->
  <last_name>Smith</last_name>
  <age>25</age>
  <address xmlns='will be dropped as well'>
    <street>21 2nd Street</street>
    <city>New York</city>
    <code>10021</code>
  </address>
  <phone type='home'>212 555-1234</phone>
</address>

Result:

["address", {"id":"1"},
  ["last_name",
    "Smith"],
  ["age",
    "25"],
  ["address",
    ["street",
      "21 2nd Street"],
    ["city",
      "New York"],
    ["code",
      "10021"]],
  ["phone", {"type":"home"},
    "212 555-1234"]]

XQuery Format[edit]

Example 1: Converts a JSON string to XQuery

Query:

let $input := '{
  "Title": "Drinks",
  "Author": [ "Jim Daniels", "Jack Beam" ]
}'
let $data := json:parse($input, map { 'format': 'xquery' })
return map:for-each($data, function($k, $v) {
  $k || ': ' || string-join($v, ', ')
})

Result:

Author: Jim Daniels, Jack Beam
Title: Drinks

Example 2: Converts XQuery data to JSON

Query:

for $item in (
  true(),
  'ABC',
  array { 1 to 5 },
  map { "Key": "Value" }
)
return json:serialize(
  $item,
  map { 'format': 'xquery', 'indent': 'no' }
)

Result:

true
"ABC"
[1,2,3,4,5]
{"Key":"Value"}

Errors[edit]

Code Description
options The specified options are conflicting.
parse The specified input cannot be parsed as JSON document.
serialize The specified node cannot be serialized as JSON document.

Changelog[edit]

Version 9.4
Version 9.1
  • Updated: json:parse can be called with empty sequence.
Version 9.0
  • Updated: map format renamed to xquery.
  • Updated: error codes updated; errors now use the module namespace
Version 8.4
  • Updated: unescape changed to escape.
Version 8.2
  • Added: Conversion format basic.
Version 8.0
  • Updated: Serialization aligned with the json output method of the official specification.
  • Added: liberal option.
  • Removed: spec option.
Version 7.8
  • Removed: json:parse-ml, json:serialize-ml.
  • Updated: json:parse now returns a document node instead of an element, or an XQuery map if format is set to .map.
Version 7.7.2

The module was introduced with Version 7.0.