| XQUERY/XSLT API DOCUMENTATION 4.2 |
All XQuery & XSLT Functions MarkLogic Built-In Functions Admin (xdmp:) AppServer (xdmp:) Classifier (cts:) Debug (dbg:) Document Conversion (xdmp:) Extension (xdmp:) Documents, Directories, Properties, and Locks Functions Extension Functions Function Values Functions HTTP Functions JSON Functions MarkLogic Server Functions Search Functions XML Functions XQuery Context Functions XSLT Functions map (map:) Math (math:) Profile (prof:) Search (cts:) cts:query Constructors Functions Geospatial Functions Geospatial Lexicon Functions Lexicon Functions Search Functions Search Clustering Functions Security (xdmp:) Server Monitoring (xdmp:) Spell (spell:) Update (xdmp:) XQuery Library Modules Admin Library (admin:) Actions Functions Appserver Functions Database Functions Forest Functions Group Functions Host Functions Mimetypes Functions Scheduler Functions Alerting (alert:) Elastic Compute Cloud (ec2:) Entity Enrichment (entity:) Flexible Replication (flexrep:) Geospatial Supporting Functions GEO Functions GeoRSS Functions GML Functions KML Functions MCGM Functions Information Studio (info:) Information Studio Developer (infodev:) Library Services (dls:) cts:query Constructor Functions Document Management Functions Document Update Functions Retention Policy Functions XInclude Functions Modular Documents XInclude Functions XPointer Functions PKI (pki:) Plugin (plugin:) Search (search:) Security-sec (sec:) Spelling Dictionary Management (spell:) Thesaurus (thsr:) Triggers (trgr:) Triggers Library (trgr:) WordProcessingML (ooxml:) ZIP Package (ooxml:) CPF Functions Content Processing Framework (cpf:) CSS Conversion (css:) DocBook Conversion (dbk:) Domains (dom:) Excel Conversion (excel:) Generic Conversion (cvt:) Links (lnk:) MSWord Conversion (msword:) PDF Conversion (pdf:) Pipelines (p:) Powerpoint Conversion (ppt:) XHTML Conversion (xhtml:) W3C-Standard Functions Accessor (fn:) AnyURI (fn:) Boolean (fn:) Context (fn:) DurationDateTime (fn:) Error and Trace (fn:) Node (fn:) Numeric (fn:) QName (fn:) Sequence (fn:) String (fn:) XSLT (fn:) W3C XQuery/XSLT Reference W3C XQuery Functions W3C XQuery Language W3C XSLT Language
This page was generated September 12, 2012 5:59 AM | XQuery & XSLT Built-In & Modules Function Reference
Built-In: Extension - JSON Functions
The JSON built-in functions serialize XQuery items to JSON and read a JSON string and create XQuery items from it. JSON (JavaScript Object Notation) is a data-interchange format popular mechanism for passing data from JavaScript to other programming environments.
Function Summary
xdmp:from-json | Parses a string as JSON, returning an item sequence.
xdmp:to-json | Returns a string representing a JSON serialization of a given item sequence.
Function Detail
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}

© Copyright 2002-2012 MarkLogic Corporation. All rights reserved.