http://www.zorba-xquery.com/modules/converters/json ZC

Module Description
Before using any of the functions below please remember to import the module namespace:
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:

In the following, we briefly describe both mappings.

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:

Author:

Sorin Nasoi

XQuery version and encoding for this module:

xquery version "3.0" encoding "utf-8";

Module Resources
  • the XQuery module can be found here.
Module Dependencies

Imported modules:

Imported schemas:

External C++ library dependencies:

Related Documentation

For more details please also see:

Namespaces
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
Function Summary
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.
Functions
parse back to 'Function Summary'
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.

Parameters:
  • $arg a sequence of valid JSON strings.
Returns:
  • a sequence of nodes according to Simple XDM-JSON mapping described above.
Errors:
  • json:ParseError if the JSON string passed as parameter is not valid JSON.
Examples:

parse back to 'Function Summary'
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.

Parameters:
  • $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.
Returns:
  • a sequence of nodes according to either one of the mappings described above.
Errors:
  • 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.
Examples:

parse-ml back to 'Function Summary'
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.

Parameters:
  • $arg a sequence of valid JSON strings.
Returns:
  • a sequence of nodes according the JSON-ML mapping described above.
Errors:
  • json:ParseError if the JSON string passed as parameter is not valid JSON.
Examples:

serialize back to 'Function Summary'
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

Parameters:
  • $xml a sequence of nodes.
Returns:
  • a JSON string.
Errors:
  • json:InvalidXDM if the input $xml is not a valid XDM representation of JSON or JSON ML.
Examples:

serialize back to 'Function Summary'
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.

Parameters:
  • $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.
Returns:
  • a JSON string.
Errors:
  • 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.
Examples:

serialize-ml back to 'Function Summary'
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.

Parameters:
  • $xml a sequence of nodes.
Returns:
  • a JSON string.
Errors:
  • json:InvalidXDM if the input $xml is not a valid XDM representation of JSON or JSON ML.
Examples:

blog comments powered by Disqus