Loading TOC...

MarkLogic Server 11.0 Product Documentation
xdmp:from-json

xdmp:from-json(
   $arg as node()
) as item()*

Summary

Atomizes a JSON node, returning a JSON value.

Parameters
arg A node of kind object-node(), array-node(), text(), number-node(), boolean-node(), null-node(), or document-node().

Usage Notes

An object-node() atomizes as a json:object.

An array-node() atomizes as a json:array.

A null-node() atomizes as an empty sequence.

A boolean-node() atomizes as an xs:boolean.

A number-node() atomizes as an xs:integer, xs:decimal, or xs:double value.

A text() node atomizes as an xs:untypedAtomic.

A document-node() atomizes the same as its root.

This function returns the same result as fn:data, but it only works on JSON nodes.

To make a JSON node from a string, use xdmp:unquote with the "format-json" option.

Example

xdmp:from-json(xdmp:unquote('["a", null, false]',(),"format-json"))
=>
json:array(
<json:array xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:json="http://marklogic.com/xdmp/json">
   <json:value>a</json:value>
   <json:value xsi:nil="true"/>
   <json:value xsi:type="xs:boolean">false</json:value>
</json:array>)
  

Example

let $string := '[{"some-key":45683}, "this is a string", 123]'
let $node := xdmp:unquote($string,(),"format-json")
return xdmp:from-json($node)
=>
json:array(
<json:array xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:json="http://marklogic.com/xdmp/json">
  <json:value>
    <json:object>
      <json:entry key="some-key">
        <json:value xsi:type="xs:integer">45683</json:value>
      </json:entry>
    </json:object>
  </json:value>
  <json:value>this is a string</json:value>
  <json:value xsi:type="xs:integer">123</json:value></json:array>)

Example

You can make changes to the json:object and json:array values returned by
xdmp:from-json.  The following uses the map:put function on the returned
json:object value to add a property, then returns the modified object
both as a value and as a node.

let $string := '[{"some-key":45683}, "this is a string", 123]'
let $node := xdmp:unquote($string,(),"format-json")
let $array := xdmp:from-json($node)
let $_ := map:put($array[1],$array[2],$array[3])
return ($array[1],xdmp:to-json($array[1]))
=>
json:object(
<json:object xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:json="http://marklogic.com/xdmp/json">
  <entry key="some-key">
    <json:value xsi:type="xs:integer">45683</json:value>
  </entry>
  <entry key="this is a string">
    <json:value xsi:type="xs:integer">123</json:value>
  </entry>
</json:object>)
{"some-key":45683, "this is a string":123}

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