Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

XQuery API JSON Functions

isubiker edited this page · 3 revisions
Clone this wiki locally

XQuery API - JSON Functions

The JSON functions in Corona support parsing, serializing and constructing JSON documents. These functions either produce or consume an XML structure that is intended to be used as an internal format only and treated as more of less of a black box.


json:parse(
    $json as xs:string,
    [$enableExtensions as xs:boolean]
) as element(json:json)

Parses a JSON string, returning an XML structure that represents the JSON.

Parameters:

  • json - The JSON string can be either an object, array, string or number.
  • enableExtensions - Optional argument to turn off or turn on the JSON extensions. The default is true, enabled.

Example:

json:parse('{"id": 123456, "name": "Bob Albud"}')
json:parse('[1, 2, 3, 4, 5]')

json:serialize(
    $internalJSON as node(),
    [$jsonPCallback as xs:string?]
) as xs:string

Serializes the internal XML format that's generated by json:parse, json:document, json:object, json:array, json:date, json:xml or json:null into a string that can be delivered to a JSON library.

Parameters:

  • internalJSON - The internal representation of the JSON to be serialized out as a JSON string
  • jsonPCallback - If specified, the serialized string will include the specified JSONP callback function

Example:

 json:serialize(json:object(("name", "Bob Albud"))) -> {"name": "Bob Albud"}
 json:serialize(json:object(("name", "Bob Albud")), "displayUser") -> displayUser({"name": "Bob Albud"})

json:document(
    $value as item()
) as element(json:json)

Turns any JSON representation (constructed or via json:parse) into a document that's ready for storage into MarkLogic.

  • value - The JSON internal representation to store into MarkLogic

Example:

json:document(json:parse('{"id": 123456, "name": "Bob Albud"}'))

json:object(
    [$keyValues as item()*]
) as element(json:item)

json:array(
    [$items as item()*]
) as element(json:item)

json:date(
    $value as xs:anySimpleType
) as element(json:item)

Because JSON doesn't have a date datatype, we have to do some special things. This function will accept either an xs:dateTime, xs:date or a date string. In the case of a date string, an attempt will be made to parse the string into an xs:dateTime. If the string cannot be parsed an error is thrown.


json:xml(
    $value as element()
) as element(json:item)

Because JSON doesn't have an xml datatype, we have to do some special things to get it to work. When serialized out as JSON the xml will appear as a string but internally it is represented as an xml tree.


json:null(
) as element(json:item)

Because XQuery doesn't have a strict null value, this function allows us to construct a JSON null. This can be useful if you need objects or arrays with null values.

Examples:

json:array((1, 2, json:null(), 4)) -> [1, 2, null, 4]
json:object(("foo", json:null())) -> {"foo": null}
Something went wrong with that request. Please try again.