Loading TOC...

json.transformFromJson

json.transformFromJson(
   $json as String,
   [$config as Object]
) as Sequence

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.

See Also

Example

const json = require('/MarkLogic/json/json.xqy');
 
let 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.transformFromJson(j)

 /* The following output is produced:
 <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>
    <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">
    </empty>
</fourthKey>
<fifthKey type="null">
</fifthKey>
<sixthKey type="array">
</sixthKey>
</json>
 */

Example

const json = require('/MarkLogic/json/json.xqy');
 
 let 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 = (config, 'attribute-names' , ('subKey' , 'boolKey' , 'empty' ))

json.transformFromJson(j, config)

/* The following output is produced:
<object>
    <firstKey>first value</firstKey>
    <secondKey>first item</secondKey>
    <secondKey>second item</secondKey>
    <secondKey>
    </secondKey>
    <secondKey>third item</secondKey>
    <secondKey>false</secondKey>
    <thirdKey>3</thirdKey>
    <fourthKey>
        <subKey>sub value</subKey>
        <boolKey>true</boolKey>
        <empty>
        </empty>
    </fourthKey>
    <fifthKey>
    </fifthKey>
</object>
 */

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