Difference between revisions of "JSON Module"
Line 54: | Line 54: | ||
| valign='top' | '''Errors''' | | valign='top' | '''Errors''' | ||
|<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0016]]</b> is raised if the specified node cannot be serialized as JSON document. | |<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0016]]</b> is raised if the specified node cannot be serialized as JSON document. | ||
+ | |} | ||
+ | |||
+ | ==json:parse-ml== | ||
+ | {| | ||
+ | |- | ||
+ | | valign='top' width='90' | '''Signatures''' | ||
+ | |<code><b>json:parse</b>($input as xs:string()) as element()</code> | ||
+ | |- | ||
+ | | valign='top' | '''Summary''' | ||
+ | |Converts the [http://jsonml.org JsonML] document specified by <code>$input</code> to XML, and returns the result as <code>element()</code> instance. The JSON input must conform to the JsonML specification to be successfully converted. | ||
+ | |- | ||
+ | | valign='top' | '''Errors''' | ||
+ | |<b>[[XQuery Errors#BaseX Errors (BASX)|BASX0015]]</b> is raised if the specified input cannot be parsed as JsonML instance. | ||
|} | |} | ||
Revision as of 00:37, 15 August 2011
JSON (JavaScript Object Notation) is a popular data exchange format for applications written in JavaScript. This module contains XQuery functions to parse and serialize JSON documents. All functions are preceded by the json:
prefix, which is linked to the http://www.basex.org/json
namespace. The proposed JSON functions will be officially supported with the upcoming Version 6.8 of BaseX.
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 two sets of functions in this module:
- json:parse and json:serialize facilitate a lossless representation of JSON to XML and back, and
- json:serialize-ml and json:parse-ml are used to transform XML to JSON and back.
json:parse
Signatures | json:parse($input as xs:string()) as element(json)
|
Summary | Converts the JSON document specified by $input to XML, and returns the result as element(json) instance. The converted XML document is both well readable and lossless, i.e., the converted document can be serialized back to the original JSON representation. The transformation is based on the following rules:
|
Errors | BASX0015 is raised if the specified input cannot be parsed as JSON document. |
json:serialize
Signatures | json:serialize($input as node()) as xs:string()
|
Summary | Serializes the node specified by $input as JSON, and returns the result as xs:string instance. The serialized node must conform to the syntax specified by the json:parse() function.XML documents in the JSON format can also be serialized if the Serialization Option "method" is set to "json" .
|
Errors | BASX0016 is raised if the specified node cannot be serialized as JSON document. |
json:serialize-ml
Signatures | json:serialize($input as node()) as xs:string()
|
Summary | Serializes the node specified by $input as described in the JsonML specification, and returns the result as xs:string instance. JsonML can be used to transform any XML document to JSON and back.Note, however, that namespaces, comments and processing instructions will be discarded in the transformation process. More details are found in the official JsonML documentation.
|
Errors | BASX0016 is raised if the specified node cannot be serialized as JSON document. |
json:parse-ml
Signatures | json:parse($input as xs:string()) as element()
|
Summary | Converts the JsonML document specified by $input to XML, and returns the result as element() instance. The JSON input must conform to the JsonML specification to be successfully converted.
|
Errors | BASX0015 is raised if the specified input cannot be parsed as JsonML instance. |
Examples
Example 1: A basic example to convert an empty JSON document
Query:
json:parse('{}')
Result:
<json/>
Example 2: Conversion of some 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": "2009-02-02T11:10:27Z", "generator": "http://www.flickr.com/" }')
Result:
<json> <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>2009-02-02T11:10:27Z</modified> <generator>http://www.flickr.com/</generator> </json>
Example 3: Globally defined data types, key rewritings
Query:
json:parse('{ "last_name": "John Smith", "age": 25, "address": { "street": "21 2nd Street", "city": "New York", "code": 10021 }, "phone": [ { "type": "home", "number": "212 555-1234" } ] }')
Result:
<json numbers="age code" arrays="phone" objects="json address value"> <last__name>John Smith</last__name> <age>25</age> <address> <street>21 2nd Street</street> <city>New York</city> <code>10021</code> </address> <phone> <value> <type>home</type> <number>212 555-1234</number> </value> </phone> </json>