|
|
json:set-item-at(
|
|
$array as json:array,
|
|
$pos as xs:double,
|
|
$value as item()*
|
| ) as empty-sequence() |
|
 |
Summary:
Sets a value in an array at a specified position.
|
Parameters:
$array
:
An array.
|
$pos
:
The position to update. Invalid positions throw XDMP-ARRAYINDEXOUTOFBOUNDS
|
$value
:
A value. Empty sequences are allowed.
|
|
Example:
let $array := json:to-array( ("a","b","c"), 5 )
let $put := json:set-item-at($array, 5 , <info>45683</info>)
return
<result>{$array}</result>
=>
<result>
<json:array xmlns:json="http://marklogic.com/xdmp/json">
<json:value xsi:type="xs:string">a</json:value>
<json:value xsi:type="xs:string">b</json:value>
<json:value xsi:type="xs:string">c</json:value>
<json:value xsi:nil="true"/>
<json:value><info>45683</info></json:value>
</json:array>
</result>
|
Example:
xquery version "1.0-ml";
let $array := json:to-array(("a", "z", "c"))
return (
$array[2],
json:set-item-at($array, 2, "b"),
$array[2]
)
=>
z
b
|
|
|
|
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:
A JSON object or string is parsed as a json:object.
The JSON null value is represented as the empty sequence, and serializes
in a way that indicates it is a null value. Therefore, when the null value
is returned to JSON via xdmp:to-json, the null value is
preserved.
Nested arrays in JSON are turned into nested sets of
json:arrays.
Any codepoints in the JSON string that aren't allowed in XML are
rejected and an error is thrown.
|
Example:
xdmp:from-json('["a", null, false]')
=>
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 xsi:type="xs:string">a</json:value>
<json:value xsi:nil="true"/>
<json:value xsi:type="xs:boolean">false</json:value>
</json:array>)
|
Example:
xquery version "1.0-ml";
let $json :=
'[{"some-key":45683}, "this is a string", 123]'
return
xdmp:from-json($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>
<json:object>
<json:entry key="some-key">
<json:value xsi:type="xs:integer">45683</json:value>
</json:entry>
</json:object>
</json:value>
<json:value xsi:type="xs:string">this is a string</json:value>
<json:value xsi:type="xs:integer">123</json:value></json:array>)
Note that what is shown above is the serialization of the XQuery items to
a json:array. 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], xdmp:to-json($items[1]))
(: returns the following json:array and JSON string:
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="123">
<json:value xsi:type="xs:string">this is a string</json:value>
</entry>
</json:object>)
{"some-key":45683, "123":"this is a string"}
This query uses the map functions to modify the first json:object
in the json:array.
:)
|
|
|
|
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}
|
|
|