Loading TOC...

json:transform-to-json

json:transform-to-json(
   $node as node(),
   [$config as map:map]
) as document-node()?

Summary

This function transforms an XML document to JSON using the default ("basic") strategy if none is supplied.

Parameters
$node The node to transform. The node must be a node that was transformed from JSON (for example, as the result of a json:transform-from-json call with the basic strategy or from JSON that was loaded using the REST API).
$config The configuration object representing the strategy.

Usage Notes

The supplied document (element or document node) is transformed to JSON using the default ("basic") strategy and returned as a document node containing a JSON node (object-node() or array-node()).

When the default "basic" strategy is used, the XML node must be in the http://marklogic.com/xdmp/json/basic namespace.

Note: Version of MarkLogic prior to 8.0.1 returned an xs:string. See https://docs.marklogic.com/guide/relnotes/chap4#id_92312

See Also

Example

(: sample.xml must be inserted first. 
For reference: 
xdmp:document-insert("/sample.xml", <a><b><attr>d</attr><LABEL>c</LABEL></b></a>) :)

xquery version "1.0-ml";
import module namespace json="http://marklogic.com/xdmp/json"
 at "/MarkLogic/json/json.xqy";
 
let $config := json:config("full") => map:with("whitespace", "ignore")
return json:transform-to-json( fn:doc("/sample.xml") , $config )
(: The following output is produced:
{
  "a": {
    "_children": [ { "b": {
          "_children": [
            {
              "attr": {
                "_children": ["d"]}
            },
            {
              "LABEL": {
                "_children": ["c"] }
            }
          ]
        }
      }
    ]
  }
}
:)
  

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

Comments

The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • The examples don't show how the function works. It would be nice to have an example XML document and see how it handles all types of nodes (including other elements, text, and attributes) in the XML.
    • Have you tried looking in the chapter on JSON?: http://docs.marklogic.com/guide/app-dev/json
  • Hello! Is it possible to "hint" the transforming function with a data type of an element/attribute content to prevent representing a number as a string in the resulting JSON string?
    • When using the 'custom' strategy, if you have a schema in scope for the XML document, it will use the type information from the schema to decide how to convert atomic values (number,string,null,boolean). The schema does not need to be complete nor does the document need to validate against the scheme, just adding the element declarations for any incorrectly handle simple types is sufficient. Using a namespace helps, otherwise a same-named element in another schema may shadow the 'intended' one.