|
|
xdmp:from-json(
|
|
$arg as xs:string
|
| ) as item()* |
|
 |
Summary:
Parses a string as JSON, returning an item sequence.
|
Parameters:
$arg
:
JSON input to be parsed.
|
|
Usage Notes:
JSON objects are parsed as maps.
The JSON null value is represented as the empty sequence.
Nested arrays in JSON are flattened out.
Any codepoints in the JSON string that aren't allowed in XML are
rejected and an error is thrown.
|
Example:
xdmp:from-json('["a", false]')
=> ("a", fn:false())
|
Example:
xquery version "1.0-ml";
let $json :=
'[{"some-key":45683}, "this is a string", 123]'
return
xdmp:from-json($json)
=>
map:map(
<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:map="http://marklogic.com/xdmp/map">
<map:entry key="some-key">
<map:value xsi:type="xs:integer">45683</map:value>
</map:entry>
</map:map>)
this is a string
123
Note that what is shown above is the serialization of the XQuery items.
You can also use some or all of the items in the XQuery data model.
For example, consider the following, which adds to the map based on the
other values:
xquery version "1.0-ml";
let $json :=
'[{"some-key":45683}, "this is a string", 123]'
let $items := xdmp:from-json($json)
let $put := map:put($items[1], xs:string($items[3]), $items[2])
return
$items[1]
(: returns the following map:
map:map(
<map:map xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:map="http://marklogic.com/xdmp/map">
<map:entry key="123">
<map:value xsi:type="xs:string">this is a string</map:value>
</map:entry>
<map:entry key="some-key">
<map:value xsi:type="xs:integer">45683</map:value>
</map:entry>
</map:map>)
:)
In the above query, the first item ($items[1]) returned from the
xdmp:from-json call is a map, and the map is then modified, and then
the modified map is returned.
|
|
|
|
xdmp:to-json(
|
|
$item as item()*
|
| ) as xs:string |
|
 |
Summary:
Returns a string representing a JSON
serialization of a given item sequence.
|
Parameters:
$item
:
The item sequence whose JSON serialization is returned.
|
|
Usage Notes:
XML nodes are serialized to JSON strings.
JSON has no serialization for infinity, not a number, and negative 0,
therefore if you try and serialize INF, -INF, NaN, or -0 as JSON, an
exception is thrown. If you want to represent these values in some way in
your serialized JSON, then you can catch the exception (with a try/catch, for
example) and provide your own value for it.
XQuery maps (map:map types) serialize to JSON name-value
pairs.
|
Example:
xdmp:to-json(("a",fn:false()))
=> ["a", false]
|
Example:
xquery version "1.0-ml";
xdmp:to-json(
xdmp:from-json('
{ "a":111 }
')
)
=>
{"a":111}
|
|
|