http://www.zorba-xquery.com/modules/converters/json
import module namespace json = "http://www.zorba-xquery.com/modules/converters/json";
In order to enable JSON processing with XQuery, Zorba implements a set of functions that open XQuery developers the door to process JSON data. Specifically, this module provides two types of functions. Functions to:
- parse JSON and convert it to XDM and
- serialize XDM in order to output JSON.
Both types of functions are available to parse and serialize two types of XDM-JSON mappings:
- the first mapping called in this document simple XDM-JSON has been proposed by John Snelson
- the second mapping is called JsonML
Simple XDM-JSON Mapping
- In order to process JSON with XQuery, Zorba implements a mapping between JSON and XML that was initially proposed by John Snelson in his article Parsing JSON into XQuery
JsonML Mapping
- JSonML (JSON Markup Language) is an application of the JSON format.
- The purpose of JSonML is to provide a compact format for transporting XML-based markup as JSon. In contrast to the simple XDM-JSON mapping described above JsonML allows a lossless conversion back and forth.
Important Notes:
- Zorba uses the Jansson library for manipulating JSON data.
- We have tested against the following Jansson library versions: available from Jansson releases.
Sorin Nasoi
xquery version "3.0" encoding "utf-8";
- the XQuery module can be found here.
Imported modules:
Imported schemas:
External C++ library dependencies:
For more details please also see:
- Mapping proposed by John Snelson
- JSonML
- Jansson library for encoding, decoding and manipulating JSON data
| err | http://www.w3.org/2005/xqt-errors |
| json | http://www.zorba-xquery.com/modules/converters/json |
| json-options | http://www.zorba-xquery.com/modules/converters/json-options |
| schema | http://www.zorba-xquery.com/modules/schema |
| ver | http://www.zorba-xquery.com/options/versioning |
|
parse
(
$arg as xs:string?
) as document(element(*,xs:untyped)) This function parses a JSON string and returns an XDM instance according to simple XDM-JSON mapping described above. |
|
|
parse
(
$arg as xs:string?,
$options as element(json-options:options)
) as document(element(*,xs:untyped)) This function parses a JSON string and returns an XDM instance according to either one of the mappings described above. |
|
|
parse-ml
(
$arg as xs:string?
) as document(element(*,xs:untyped)) This function parses a JSON string and returns an XDM instance according to JsonML mapping described above. |
|
|
serialize
(
$xml as item()*
) as xs:string |
|
|
serialize
(
$xml as item()*,
$options as element(json-options:options)
) as xs:string The serialize function takes a sequence of nodes as parameter and transforms each element into a valid JSON string according to one of the mappings described above. |
|
|
serialize-ml
(
$xml as item()*
) as xs:string The serialize function takes a sequence of nodes as parameter and transforms each element into a valid JSON string according to the JsonML mapping described above. |
declare function json:parse (
$arg as xs:string?
) as document(element(*,xs:untyped))
This function parses a JSON string and returns an XDM instance according to simple XDM-JSON mapping described above.
- $arg a sequence of valid JSON strings.
- a sequence of nodes according to Simple XDM-JSON mapping described above.
- json:ParseError if the JSON string passed as parameter is not valid JSON.
declare function json:parse ( $arg as xs:string?, $options as element(json-options:options) ) as document(element(*,xs:untyped))
This function parses a JSON string and returns an XDM instance according to either one of the mappings described above.
- $arg a sequence of valid JSON strings.
- $options a set of name and value pairs that provide options to configure the JSON mapping process that have to be validated against the "http://www.zorba-xquery.com/modules/converters/json-options" schema.
- a sequence of nodes according to either one of the mappings described above.
- err:XQDY0027 if $options can not be validated against the json-options schema
- json:ParseError if the JSON string passed as parameter is not valid JSON.
declare function json:parse-ml (
$arg as xs:string?
) as document(element(*,xs:untyped))
This function parses a JSON string and returns an XDM instance according to JsonML mapping described above.
- $arg a sequence of valid JSON strings.
- a sequence of nodes according the JSON-ML mapping described above.
- json:ParseError if the JSON string passed as parameter is not valid JSON.
declare function json:serialize (
$xml as item()*
) as xs:string
The serialize function takes a sequence of nodes as parameter and transforms each element into a valid JSON string according to the Simple XDM-JSON mapping described above
- $xml a sequence of nodes.
- a JSON string.
- json:InvalidXDM if the input $xml is not a valid XDM representation of JSON or JSON ML.
declare function json:serialize ( $xml as item()*, $options as element(json-options:options) ) as xs:string
The serialize function takes a sequence of nodes as parameter and transforms each element into a valid JSON string according to one of the mappings described above.
- $xml a sequence of nodes.
- $options a set of name and value pairs that provide options to configure the JSON mapping process that have to be validated against the "http://www.zorba-xquery.com/modules/converters/json-options" schema.
- a JSON string.
- err:XQDY0027 if $options can not be validated against the json-options schema
- json:InvalidXDM if the input $xml is not a valid XDM representation of JSON or JSON ML.
declare function json:serialize-ml (
$xml as item()*
) as xs:string
The serialize function takes a sequence of nodes as parameter and transforms each element into a valid JSON string according to the JsonML mapping described above.
- $xml a sequence of nodes.
- a JSON string.
- json:InvalidXDM if the input $xml is not a valid XDM representation of JSON or JSON ML.
