Changes

Jump to navigation Jump to search
1,084 bytes added ,  14:38, 16 April 2019
This [[Module Library|XQuery Module]] contains functions to parse and serialize JSON documents. data [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, 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=
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==
 
'''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===
** The existing types are ''string'', ''number'', ''boolean'', ''null'', ''object'', and ''array''.
** 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===
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===XQuery=== The {{Code|xquery}} format is lossless, if you select too. It converts JSON Parsing data to an XQuery value (a map, array, string, number, boolean, or empty sequence) and switch to vice versa. The conversion rules are the ''Parsing'' tabsame as for [[XQuery 3.1#fn:parse-json|fn:parse-json]]. The resulting representation consumes less memory than XML-based formats, you and values can see the effects of some of the be directly accessed without conversion options. Thus, it is recommendable for very large inputs and for efficient ad-hoc processing.
==Options==
| {{Code|format}}
| Specifies the format for converting JSON data.
| {{Code|direct}}, {{Code|attributes}}, {{Code|jsonml}}, {{Code|mapxquery}}
| {{Code|direct}}
| ''parse'', ''serialize''
| {{Code|merge}}
| This option is considered when {{Code|direct}} or {{Code|attributes}} conversion is used:<br/>
* If a name has the same type throughout the documentdata, 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.
| {{Code|yes}}, {{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 document specified by {{Code|$inputstring}} to an XML document or a mapXQuery 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|BXJS0001parse|#Errors}} the specified input cannot be parsed as JSON document.<br/>{{Error|options|#Errors}} the specified options are conflicting.
|}
|-
| 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 node specified by {{Code|$input}} as JSON, and returns using the result as specified {{Code|xs:string$options}} instance. , and returns the result as string:* The node input is expected to conform to the output results that are created by the [[#json:parse|json:parse()]] function. All other * 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'''
|{{Error|BXJS0002serialize|#Errors}} the specified node cannot be serialized as JSON document.
|}
==JsonML Format==
'''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)
</pre>
'''Example 2: Converts a simple XML fragment to the JsonML format''' '''Query:'''<pre class="brush:xquery">json:serialize(<xml/>, map { 'format': 'jsonml' })</pre> '''Result:'''<pre>["xml"]</pre> '''Example 3: Converts an XML document with elements and text'''
'''Query:'''
</pre>
'''Example 43: Converts a document with nested elements and attributesto JsonML'''
'''Query:'''
["phone", {"type":"home"},
"212 555-1234"]]
</pre>
 
==XQuery Format==
 
'''Example 1: Converts a JSON string to XQuery'''
 
'''Query:'''
<pre class="brush: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, ', ')
})
</pre>
 
'''Result:'''
<pre>
Author: Jim Daniels, Jack Beam
Title: Drinks
</pre>
 
'''Example 2: Converts XQuery data to JSON'''
 
'''Query:'''
<pre class="brush:xquery">
for $item in (
true(),
'ABC',
array { 1 to 5 },
map { "Key": "Value" }
)
return json:serialize(
$item,
map { 'format': 'xquery', 'indent': 'no' }
)
</pre>
 
'''Result:'''
<pre>
true
"ABC"
[1,2,3,4,5]
{"Key":"Value"}
</pre>
|Description
|-
|{{Code|BXJS0001options}}|The specified options are conflicting.|-|{{Code|parse}}
|The specified input cannot be parsed as JSON document.
|-
|{{Code|BXJS0002serialize}}
|The specified node cannot be serialized as JSON document.
|}
=Changelog=
 
; 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
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu