Changes

Jump to navigation Jump to search
This [[Module Library|XQuery Module]] contains functions to parse and serialize JSON data [httphttps://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=
 
{{Mark|Updated with Version 9.0:}}
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/>
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. * Object pairs are represented via elements. The name of a pair is rewritten to an element name:** Empty names are represented by a single underscore ({{Code|_}}). Existing underscores are rewritten to two underscores ({{Code|__}})encoded, and characters that are not valid as described in element names are rewritten to an underscore and the character’s four-digit Unicode.** If the {{Code[[Conversion Module#Keys|lax}} option is set to {{Code|true}}Conversion Module]], invalid characters are simply replaced with underscores or (when invalid and used 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. , with {{Code|_}} is used as element name.
* Object and array values are stored in text nodes.
* The types of values are represented via {{Code|type}} attributes:
The {{Code|attributes}} format is lossless, too. The transformation based on the following rules:
* 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.* Array entries are represented via {{Code|<item>}} elements.
* Object and array values are stored in text nodes.
* The types of values are represented via {{Code|type}} attributes:
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.
 
Before {{Version|9.0}}, the format was called {{Code|map}} (it has been renamed because the resulting value may also be a string, number, boolean, or an empty sequence).
==Options==
|- valign="top"
| {{Code|liberal}}
| Determines if minor deviations from [httphttps://www.rfc-editor.org/rfc/rfc7159.txt RFC 7159] will be ignored.
| {{Code|yes}}, {{Code|no}}
| {{Code|no}}
|-
| width='120' | '''Signatures'''
|{{Func|json:parse|$input string as xs:string?|elementitem(json)?}}<br/>{{Func|json:parse|$input string as xs:string?, $options as map(xs:string, xs:string*)?|item()?}}
|-
| '''Summary'''
|Converts the JSON data specified by {{Code|$inputstring}} 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'''|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'''
|-
| 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}}
|-
| '''Summary'''
|Serializes the specified {{Code|$input}} as JSON, and returns using the result as specified {{Code|xs:string$options}} instance. , 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 for in the [[XQuery 3.1#JSON Serialization|json output method]] of the official specificationrecommendation.<br />Items Values can also be serialized as JSON if with the standard [[Serialization|Serialization Parameter]] feature of XQuery:* The parameter {{Code|method}} is needs to be set to {{Code|json}}.<br/>The , and* the options presented in this article need to be assigned to the {{Code|$optionsjson}} argument can be used to control the way the input is serializedparameter.
|-
| '''Errors'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $database := "database"
for $name in file:list('.', false(), '*.json')
let $json := json:parse($file)
return db:add($database, $json, $name)
</presyntaxhighlight>
'''Example 2: Converts a simple JSON string to XML and back'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:parse('{}')
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<json type="object"/>
</presyntaxhighlight>
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
(: serialize result as plain text :)
declare option output:method 'text';
json:serialize(<json type="object"/>)
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xquery">
{ }
</presyntaxhighlight>
'''Example 3: Converts a JSON string with simple objects and arrays'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:parse('{
"title": "Talk On Travel Pool",
"generator": "http://www.flickr.com/"
}')
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<json type="object">
<title>Talk On Travel Pool</title>
<generator>http://www.flickr.com/</generator>
</json>
</presyntaxhighlight>
'''Example 4: Converts a JSON string with different data types'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $options := map { 'merge': true() }
return json:parse('{
]
}', $options)
</presyntaxhighlight>
'''Result:'''
<pre classsyntaxhighlight lang="brush:xml">
<json numbers="age code" arrays="phone" objects="json address value">
<first__name>John</first__name>
</phone>
</json>
</presyntaxhighlight>
==JsonML Format==
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $doc in collection('json')
let $name := document-uri($doc)
let $json := json:serialize($doc, map { 'format': 'jsonml' })
return file:write($name, $json)
</presyntaxhighlight>
'''Example 2: Converts an XML document with elements and text'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:serialize(doc('flickr.xml'), map { 'format': 'jsonml' })
</presyntaxhighlight>
'''flickr.xml:'''
<pre classsyntaxhighlight lang="brush:xml">
<flickr>
<title>Talk On Travel Pool</title>
<generator>http://www.flickr.com/</generator>
</flickr>
</presyntaxhighlight>
'''Result:'''
<presyntaxhighlight lang="json">
["flickr",
["title",
["generator",
"http://www.flickr.com/"]]
</presyntaxhighlight>
'''Example 3: Converts a document with nested elements and attributes to JsonML'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
json:serialize(doc('input.xml'), map { 'format': 'jsonml' })
</presyntaxhighlight>
'''input.xml:'''
<pre classsyntaxhighlight lang="brush:xml">
<address id='1'>
<!-- comments will be discarded -->
<phone type='home'>212 555-1234</phone>
</address>
</presyntaxhighlight>
'''Result:'''
<presyntaxhighlight lang="json">
["address", {"id":"1"},
["last_name",
["phone", {"type":"home"},
"212 555-1234"]]
</presyntaxhighlight>
==XQuery Format==
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
let $input := '{
"Title": "Drinks",
$k || ': ' || string-join($v, ', ')
})
</presyntaxhighlight>
'''Result:'''
<presyntaxhighlight lang="json">
Author: Jim Daniels, Jack Beam
Title: Drinks
</presyntaxhighlight>
'''Example 2: Converts XQuery data to JSON'''
'''Query:'''
<pre classsyntaxhighlight lang="brush:xquery">
for $item in (
true(),
map { 'format': 'xquery', 'indent': 'no' }
)
</presyntaxhighlight>
'''Result:'''
<presyntaxhighlight lang="json">
true
"ABC"
[1,2,3,4,5]
{"Key":"Value"}
</presyntaxhighlight>
=Errors=
 
{{Mark|Updated with Version 9.0:}}
{| class="wikitable" width="100%"
=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
Bureaucrats, editor, reviewer, Administrators
12,024

edits

Navigation menu