Loading TOC...

MarkLogic 9 Product Documentation
json.config

json.config(
   strategy as String
) as Object

Summary

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.

Usage Notes

Use this function to when you want to use a custom strategy. Supported strategies are 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 properties

Property

Description

Default value

json-attributes

The member name used to contain attributes.

_attributes

json-children

The member name used to contain the children array.

_children

whitespace

How to handle XML text values that only contain whitespace when converting to JSON. Allowed values: "preserve", "ignore". Default: "preserve".

empty string

custom strategy properties

Property

Description

Direction

Type

Default value

array-element-names

A list of XML element names which will be treated as an Array in JSON. These can be xs:QName or xs:string. If an xs:string is used then the default namespace is used to construct the QName.

XML to JSON

[xs.QName | string]

[]

ignore-element-names

A list of XML element names which will be ignored when transformed to JSON. These can be xs:QName or xs:string. If an xs:string is used then the default namespace is used to construct the QName.

XML to JSON

(xs:QName | xs:string)*

()

ignore-attribute-names

A list of XML attribute names which will be ignored when transformed to JSON. These can be xs:QName or xs:string. If an xs:string is used then the default namespace is used to construct the QName.

XML to JSON

(xs:QName | xs:string)*

()

text-value

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

"_value"

attribute-names

A list of JSON names which are converted to XML attribute.

JSON to XML

xs:string*

()

camel-case

A boolean value indicating if camel case transformation is applied to all element and attribute names when translating to and from JSON.

BOTH

xs:boolean

fn:false()

element-namespace

The default namespace for elements.

JSON to XML

xs:string

()

element-namespace-prefix

The default prefix for elements.

JSON to XML

xs:string

()

attribute-namespace

The default namespace for attributes.

JSON to XML

xs:string

()

attribute-namespace-prefix

The default prefix for attributes.

JSON to XML

xs:string

()

attribute-prefix

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

()

full-element-names

A list of XML element names which will be treated as an full expansion in JSON similar to the full strategy. These can be xs:QName or xs:string. If an xs:string is used then the default namespace is used to construct the QName.

BOTH

(xs:QName | xs:string)*

()

json-attributes

The member name used to contain attributes when using the full-element-names expansion.

xs:string

"_attributes"

json-children

The member name used to contain the children array when using the full-element-names expansion.

xs:string

"_children"

whitespace

How to handle XML text values that only contain whitespace when converting to JSON. Allowed values: "preserve", "ignore". Default: "preserve".

xs:string

""

See Also

Example

/* sample.xml must be inserted first. 
For reference: 
declareUpdate();
xdmp.documentInsert('/sample.xml', fn.head(xdmp.unquote('<a><b><attr>d</attr><LABEL>c</LABEL></b></a>'))); */

const json = require('/MarkLogic/json/json.xqy');

let c = json.config('full');

json.transformToJson( cts.doc('/sample.xml').root, c)

/* The following output is produced:
{
  "a": {
    "_children": [ { "b": {
          "_children": [
            {
              "attr": {
                "_children": ["d"] } },
            {
              "LABEL": {
                "_children": ["c"] }
            } 
          ]
        }
      }
    ]
  }
}
 */

Example

const json = require('/MarkLogic/json/json.xqy');

const doc =  {'a':[{'b':[{'attr':'d', 'LABEL':'c'}]}]};

const config = json.config('custom')

config['array-element-names'] = [xs.QName('a'),xs.QName('b')]
config['attribute-names'] = 'attr'
config['text-value'] = 'LABEL'
var j = json.transformFromJson(doc, config);
[json.transformFromJson(doc, config), json.transformToJson(j, config)]

/* 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\">c</b></a>", 
{"a": [{"b": [{"attr": "d", "LABEL": "c"}]}]}
]

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