Loading TOC...

json:transform-from-json

json:transform-from-json(
   $json as item(),
   [$config as map:map]
) as item()*

Summary

This function transforms a JSON document to XML using the default ("basic") strategy.

Parameters
json The JSON document to transform. This is typically an xs:string but also accepts a document, object-node(), array-node(), map:map, json:array,element(map:map) , element(json:array) and element(json:object)
config The configuration object

Usage Notes

The supplied JSON document (as a string, element, object or array) is transformed to XML using the default (basic) strategy and returned as an element.

Example

xquery version "1.0-ml";
import module namespace json="http://marklogic.com/xdmp/json"
 at "/MarkLogic/json/json.xqy";
 
 declare variable $j :=  '{
    "first Key":"first value",
    "secondKey":["first item","second item",null,"third item",false],
    "thirdKey":3,
    "fourthKey":{"subKey":"sub value", "boolKey" : true, "empty" : null }
    ,"fifthKey": null,
    "sixthKey" : []
    }'  ;
 json:transform-from-json( $j)
 
     ==>

<json type="object" xmlns="http://marklogic.com/xdmp/json/basic">
  <first_20_Key type="string">first value</first_20_Key>
  <secondKey type="array">
    <item type="string">first item</item>
    <item type="string">second item</item>
    <item type="null"/>
    <item type="string">third item</item>
    <item type="boolean">false</item>
  </secondKey>
  <thirdKey type="number">3</thirdKey>
  <fourthKey type="object">
    <subKey type="string">sub value</subKey>
    <boolKey type="boolean">true</boolKey>
    <empty type="null"/>
  </fourthKey>
  <fifthKey type="null"/>
  <sixthKey type="array"/>
</json>

Example

xquery version "1.0-ml";
import module namespace json="http://marklogic.com/xdmp/json"
 at "/MarkLogic/json/json.xqy";
 
 declare variable $j :=  '{
   "object" : {
    "firstKey":"first value",
    "secondKey":["first item","second item","","third item",false],
    "thirdKey":3,
    "fourthKey":{"subKey":"sub value", "boolKey" : true, "empty" : null }
    ,"fifthKey": null,
    "sixthKey" : []
     }}'  ;

 let $config := json:config("custom") ,
          $cx := map:put( $config, "attribute-names" , ("subKey" , "boolKey" , "empty" ) )
 return 

json:transform-from-json( $j, $config )

    ==>
<object>
  <firstKey>first value</firstKey>
  <secondKey>first item</secondKey>
  <secondKey>second item</secondKey>
  <secondKey/>
  <secondKey>third item</secondKey>
  <secondKey>false</secondKey>
  <thirdKey>3</thirdKey>
  <fourthKey subKey="sub value" boolKey="true" empty=""/>
  <fifthKey/>
</object>

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.