http://zorba.io/modules/json-xml

Description

Before using any of the functions below please remember to import the module namespace:

import module namespace jx = "http://zorba.io/modules/json-xml";
Using this module, you can parse JSON data into XML, manipulate it like any other XML data using XQuery, and serialize the result back as JSON.

There are many ways to represent JSON data in XML, some loss-less ("round tripable") and some lossy ("one way"). Loss-less representations preserve the JSON data types boolean, number, and null; lossy representations convert all data to strings.

For a loss-less representation, this module implements that proposed by John Snelson. For example:
   {
     "firstName" : "John",
     "lastName" : "Smith",
     "address" : {
       "streetAddress" : "21 2nd Street",
       "city" : "New York",
       "state" : "NY",
       "postalCode" : 10021
     },
     "phoneNumbers" : [ "212 732-1234", "646 123-4567" ]
   }
 
would be represented as:
   <json type="object">
     <pair name="firstName" type="string">John</pair>
     <pair name="lastName" type="string">Smith</pair>
     <pair name="address" type="object">
       <pair name="streetAddress" type="string">21 2nd Street</pair>
       <pair name="city" type="string">New York</pair>
       <pair name="state" type="string">NY</pair>
       <pair name="postalCode" type="number">10021</pair>
     </pair>
     <pair name="phoneNumbers" type="array">
       <item type="string">212 732-1234</item>
       <item type="string">646 123-4567</item>
     </pair>
   </json>
 
For a lossy representation, this module implements JsonML (the array form). For example:
   [ "person",
     { "created" : "2006-11-11T19:23",
       "modified" : "2006-12-31T23:59" },
     [ "firstName", "Robert" ],
     [ "lastName", "Smith" ],
     [ "address",
       { "type" : "home" },
       [ "street", "12345 Sixth Ave" ],
       [ "city", "Anytown" ],
       [ "state", "CA" ],
       [ "postalCode", "98765-4321" ]
     ]
   ]
 
would be represented as:
   <person created="2006-11-11T19:23" modified="2006-12-31T23:59">
     <firstName>Robert</firstName>
     <lastName>Smith</lastName>
     <address type="home">
       <street>12345 Sixth Ave</street>
       <city>Anytown</city>
       <state>CA</state>
       <postalCode>98765-4321</postalCode>
     </address>
   </person>
 

Module code

Here is the actual XQuery module code.

Authors

Paul J. Lucas

Version Declaration

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

Namespaces

errhttp://www.w3.org/2005/xqt-errors
jxhttp://zorba.io/modules/json-xml
verhttp://zorba.io/options/versioning
zerrhttp://zorba.io/errors

Function Summary

json-to-xml($json as json-item()?, $options as object()) as element(*,xs:untyped)?

Converts JSON data into an XDM instance using one of the representations described above.

json-to-xml($json as json-item()?) as element(*,xs:untyped)?

Converts JSON data into an XDM instance using the Snelson representation described above.

xml-to-json($xml as item()*, $options as object()) as json-item()*

Converts XML data into a JSON item using one of the respresentations described above.

xml-to-json($xml as item()*) as json-item()*

Converts XML data into a JSON item using the Snelson representation described above.

json-to-xml-internal($json as json-item()?, $options as object()) as element(*)* external

xml-to-json-internal($xml as item()*, $options as object()) as json-item()* external

Functions

json-to-xml#2

declare function jx:json-to-xml(
    $json as json-item()?,
    $options as object()
) as element(*,xs:untyped)?
Converts JSON data into an XDM instance using one of the representations described above.

Parameters

  • $json

    The JSON data.

  • $options

    The JSON conversion options, for example: { "json-format" : "JsonML-array" }

Returns

  • element(*,xs:untyped)?

    said XDM instance.

Examples

json-to-xml#1

declare function jx:json-to-xml(
    $json as json-item()?
) as element(*,xs:untyped)?
Converts JSON data into an XDM instance using the Snelson representation described above.

Parameters

  • $json

    The JSON data.

Returns

  • element(*,xs:untyped)?

    said XDM instance.

Examples

xml-to-json#2

declare function jx:xml-to-json(
    $xml as item()*,
    $options as object()
) as json-item()*
Converts XML data into a JSON item using one of the respresentations described above.

Parameters

  • $xml

    The XML data to convert.

  • $options

    The conversion options, for example: { "json-format" : "JsonML-array" }

Returns

  • json-item()*

    said JSON items.

Errors

  • zerr:ZJSE0001

    if $xml is not a document or element node.

  • zerr:ZJSE0002

    if $xml contains an element that is missing a required attribute.

  • zerr:ZJSE0003

    if $xml contains an attribute having an illegal value.

  • zerr:ZJSE0004

    if $xml contains an illegal element. type.

  • zerr:ZJSE0007

    if $xml contains an element that is missing a required value.

  • zerr:ZJSE0008

    if $xml contains an illegal value for a JSON type.

Examples

xml-to-json#1

declare function jx:xml-to-json(
    $xml as item()*
) as json-item()*
Converts XML data into a JSON item using the Snelson representation described above.

Parameters

  • $xml

    The XML data to convert.

Returns

  • json-item()*

    said JSON items.

Errors

  • zerr:ZJSE0001

    if $xml is not a document or element node.

  • zerr:ZJSE0002

    if $xml contains an element that is missing a required attribute.

  • zerr:ZJSE0003

    if $xml contains an attribute having an illegal value.

  • zerr:ZJSE0004

    if $xml contains an illegal element. type.

  • zerr:ZJSE0007

    if $xml contains an element that is missing a required value.

  • zerr:ZJSE0008

    if $xml contains an illegal value for a JSON type.

Examples

json-to-xml-internal#2

declare %:private function jx:json-to-xml-internal(
    $json as json-item()?,
    $options as object()
) as element(*)* external

Returns

  • element(*)*

xml-to-json-internal#2

declare %:private function jx:xml-to-json-internal(
    $xml as item()*,
    $options as object()
) as json-item()* external

Returns

  • json-item()*