json:config( $strategy as xs:string ) as map:map
This function creates a configuration object for a specified strategy.
Parameters | |
---|---|
strategy | The name of
the strategy. One of basic (simple default JSON
conversion, starting from JSON), full (starting from
XML), or custom . |
basic
, full
,
custom
.
The basic
strategy is used if you supply no
parameters and has no user customizable properties.
The following properties are user customizable to configure the
behavior of the transformation.
full
strategy propertiesProperty |
Description |
Default value |
---|---|---|
|
The member name used to contain attributes. |
|
|
The member name used to contain the children array. |
|
|
How to handle XML text values that only contain whitespace when converting to JSON. Allowed values: "preserve", "ignore". Default: "preserve". |
empty string |
custom
strategy propertiesProperty |
Description |
Direction |
Type |
Default value |
---|---|---|---|---|
|
A list of XML element names which will be
treated as an Array in JSON. These can be
|
XML to JSON |
|
() |
|
A list of XML element names which will be
ignored when transformed to JSON. These can be
|
XML to JSON |
|
() |
|
A list of XML attribute names which will be
ignored when transformed to JSON. These can be
|
XML to JSON |
|
() |
|
When converting from XML to JSON, the JSON property name to use for the text value of an element that contains both attributes and text. When converting from JSON to XML, the JSON property name whose value should become the element text value for an element that contains both attributes and text. |
BOTH |
xs:string |
|
|
A list of JSON names which are converted to XML attribute. |
JSON to XML |
|
() |
|
A boolean value indicating if camel case transformation is applied to all element and attribute names when translating to and from JSON. |
BOTH |
|
|
|
The default namespace for elements. |
JSON to XML |
|
() |
|
The default prefix for elements. |
JSON to XML |
|
() |
|
The default namespace for attributes. |
JSON to XML |
|
() |
|
The default prefix for attributes. |
JSON to XML |
xs:string |
() |
|
If specified then when converting to JSON attributes are prefixed with this string. When converting to XML all names starting with this prefix are converted to attributes with the prefix removed. |
BOTH |
xs:string |
() |
|
A list of XML element names which will
be treated as an |
BOTH |
(xs:QName | xs:string)* |
() |
|
The member name used to contain attributes when using the
|
xs:string |
|
|
|
The member name used to contain the children array when
using the |
|
|
|
|
How to handle XML text values that only contain whitespace when converting to JSON. Allowed values: "preserve", "ignore". Default: "preserve". |
|
|
"" |
(: 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 $c := json:config("full") return json:transform-to-json( fn:doc("/sample.xml") , $c ) (: The following output is produced: { "a": { "_children": [ { "b": { "_children": [ { "attr": { "_children": ["d"] } }, { "LABEL": { "_children": ["c"] } } ] } } ] } } :)
xquery version "1.0-ml"; import module namespace json = "http://marklogic.com/xdmp/json" at "/MarkLogic/json/json.xqy"; declare variable $doc := <a><b attr="d">c</b></a>; let $c := json:config("custom") , $_ := map:put( $c, "array-element-names", (xs:QName("a"),xs:QName("b")) ), $_ := map:put( $c, "attribute-names", ("attr" ) ), $_ := map:put( $c, "text-value", "LABEL" ), $j := json:transform-to-json($doc ,$c ), $x := json:transform-from-json($j,$c) return ($j, $x) (: The JSON property name "LABEL" is used to hold the text value from the element <b/>. Without the "text-value" option, the property name would be "_value". The query produces the following output: {"a":[{"b":[{"attr":"d", "LABEL":"c"}]}]} <a><b attr="d">c</b></a> :)
Stack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.