JSON Module

From BaseX Documentation
Revision as of 22:56, 12 August 2011 by CG (talk | contribs) (Created page with "[http://www.json.org/ JSON] (JavaScript Object Notation) is a popular data exchange format for applications written in JavaScript. This module contains [[Querying#Functions|XQuer...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 json:parse() function converts a JSON document to XML. 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 transformatio is based on the following rules:

  1. The resulting document has a <json/> root node.
  2. JSON keys are represented as elements:
    1. Empty keys are represented with an underscore (_). Values of arrays (which have no keys) will also be enclosed by underscore tag names.
    2. Underscores are represented with two underscores (__).
    3. Spaces are represented with two underscores (___)
    4. Characters that cannot be represented as NCName character are represented with a leading and trailing underscore and a four-digit Unicode.
  3. The types of value are represented in attributes:
    1. The value types number, boolean, null and array are represented with a type attribute.
    2. String and object types are omitted as they are treated as default types for flat elements and and nested elements with/without names.
    3. If a key has the same type throughout the whole document, the type attribute will be omitted. Instead, the key will be listed in additional, type-specific attributes in the root node. The attributes are named by their type (number, boolean, null or array) and will contain all relevant keys as value, separated by whitespaces.

The proposed JSON functions will be available with Version 6.8 of BaseX.

json:parse

Signatures json:parse($input as xs:string()) as element(json)
Summary Converts the JSON document specified by $input to XML, following the rules described in the introduction, and returns the result as element(json) instance.

Example

Query:

json:parse('{}')

Result:

<json/>