<html>
<head>
    <base href="https://docs.jboss.org/author">
            <link rel="stylesheet" href="/author/s/en/2172/19/5/_/styles/combined.css?spaceKey=TEIID&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://docs.jboss.org/author/display/TEIID/JSON+Functions">JSON Functions</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://docs.jboss.org/author/display/~shawkins">Steven Hawkins</a>
    </h4>
        <br/>
                         <h4>Changes (3)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code:SQL}JSONARRAY(value...){code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >value is any object [convertable to a JSON|#Conversion to JSON] value. Return value is <span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">a </span>clob<span class="diff-added-chars"style="background-color: #dfd;"> marked as being valid JSON</span>.</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Null values will be included in the result as null literals. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h1. JSONOBJECT <br> <br>Returns a JSON object. <br> <br>{code:SQL}JSONARRAY(value [as name] ...){code} <br> <br>value is any object [convertable to a JSON|#Conversion to JSON] value. Return value is a clob marked as being valid JSON. <br> <br>Null values will be included in the result as null literals. <br> <br>If a name is not supplied and the expression is a column reference, the column name will be used otherwise exprN will be used where N is the 1-based index of the value in the JSONARRAY expression. <br> <br>{code:sql title=mixed value example} <br>jsonObject(&#39;a&quot;b&#39; as val, 1, null as &quot;null&quot;) <br>{code}Would return{code} <br>{&quot;val&quot;:&quot;a\&quot;b&quot;,&quot;expr2&quot;:1,&quot;null&quot;:null} <br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h1. JSONPARSE <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{code:SQL}JSONPARSE(value, wellformed){code} <br> <br></td></tr>
            <tr><td class="diff-changed-lines" >value is blob with an appropriate JSON binary encoding (UTF-8, UTF-16, or UTF-32) or a clob. wellformed is a boolean indicating that validation should be skipped.  Return value is <span class="diff-added-words"style="background-color: #dfd;">a</span> clob marked as being valid JSON. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>A null for either input will return null. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>JSON functions provide functionality for working with JSON (Javascript Object Notation) data. </p>

<h1><a name="JSONFunctions-JSONTOXML"></a>JSONTOXML</h1>

<p>Returns an xml document from JSON.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">JSONTOXML(rootElementName, json)</pre>
</div></div>

<p>rootElementName is a string, json is in {clob, blob}. Return value is xml.</p>

<p>The appropriate UTF encoding (8, 16LE. 16BE, 32LE, 32BE) will be detected for JSON blobs.  If another encoding is used, see the to_chars function.</p>

<p>The result is always a well-formed XML document.</p>

<p>The mapping to XML uses the following rules:</p>

<ul>
        <li>The current element name is initially the rootElementName, and becomes the object value name as the JSON structure is traversed.</li>
</ul>


<ul>
        <li>All element names must be valid xml 1.1 names.  Invalid names are fully escaped according to the SQLXML specification.</li>
</ul>


<ul>
        <li>Each object or primitive value will be enclosed in an element with the current name.</li>
</ul>


<ul>
        <li>Unless an array value is the root, it will not be enclosed in an additional element.</li>
</ul>


<ul>
        <li>Null values will be represented by an empty element with the attribute xsi:nil="true"</li>
</ul>


<ul>
        <li>Boolean and numerical value elements will have the attribute xsi:type set to boolean and decimal respectively.</li>
</ul>



<p>JSON:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Sample JSON to XML for jsonToXml('person', x)</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">{"firstName" : "John" , "children" : [ "Randy", "Judy" ]}
</pre>
</div></div>

<p>XML:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Sample JSON to XML for jsonToXml('person', x)</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: xml; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
&lt;?xml version="1.0" ?&gt;
   &lt;person&gt;
      &lt;firstName&gt;John&lt;/firstName&gt;
      &lt;children&gt;Randy&lt;/children&gt;
      &lt;children&gt;Judy&lt;children&gt;
   &lt;/person&gt;
</pre>
</div></div>

<p>JSON:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Sample JSON to XML for jsonToXml('person', x) with a root array.</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
[{"firstName" : "George" }, { "firstName" : "Jerry" }]
</pre>
</div></div>

<p>XML (Notice there is an extra "person" wrapping element to keep the XML well-formed):</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Sample JSON to XML for jsonToXml('person', x) with a root array.</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: xml; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
&lt;?xml version="1.0" ?&gt;
&lt;person&gt;
  &lt;person&gt;
    &lt;firstName&gt;George&lt;/firstName&gt;
  &lt;/person&gt;
  &lt;person&gt;
    &lt;firstName&gt;Jerry&lt;/firstName&gt;
  &lt;/person&gt;
&lt;/person&gt;
</pre>
</div></div>

<p>JSON:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Sample JSON to XML for jsonToXml('root', x) with an invalid name.</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
{"/invalid" : "abc" }
</pre>
</div></div>


<p>XML:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader" style="border-bottom-width: 1px;"><b>Sample JSON to XML for jsonToXml('root', x) with an invalid name.</b></div><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: xml; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
&lt;?xml version="1.0" ?&gt;
&lt;root&gt;
  &lt;_u002F_invalid&gt;abc&lt;/_u002F_invalid&gt;
&lt;/root&gt;
</pre>
</div></div>

<h1><a name="JSONFunctions-JSONARRAY"></a>JSONARRAY</h1>

<p>Returns a JSON array.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">JSONARRAY(value...)</pre>
</div></div>

<p>value is any object <a href="#JSONFunctions-ConversiontoJSON">convertable to a JSON</a> value. Return value is a clob marked as being valid JSON.</p>

<p>Null values will be included in the result as null literals.</p>

<p><div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
jsonArray('a"b', 1, null, false, {d'2010-11-21'})
</pre>
</div></div>Would return<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
["a\"b",1,null,false,"2010-11-21"]
</pre>
</div></div></p>

<h1><a name="JSONFunctions-JSONOBJECT"></a>JSONOBJECT</h1>

<p>Returns a JSON object.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">JSONARRAY(value [as name] ...)</pre>
</div></div>

<p>value is any object <a href="#JSONFunctions-ConversiontoJSON">convertable to a JSON</a> value. Return value is a clob marked as being valid JSON.</p>

<p>Null values will be included in the result as null literals.</p>

<p>If a name is not supplied and the expression is a column reference, the column name will be used otherwise exprN will be used where N is the 1-based index of the value in the JSONARRAY expression.</p>

<p><div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
jsonObject('a"b' as val, 1, null as "null")
</pre>
</div></div>Would return<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
{"val":"a\"b","expr2":1,"null":null}
</pre>
</div></div></p>

<h1><a name="JSONFunctions-JSONPARSE"></a>JSONPARSE</h1>

<p>Validates and returns a JSON result.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: sql; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">JSONPARSE(value, wellformed)</pre>
</div></div>

<p>value is blob with an appropriate JSON binary encoding (UTF-8, UTF-16, or UTF-32) or a clob. wellformed is a boolean indicating that validation should be skipped.  Return value is a clob marked as being valid JSON.</p>

<p>A null for either input will return null.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Confluence; brush: java; gutter: false" style="font-size:12px; font-family: ConfluenceInstalledFont,monospace;">
jsonParse('"a"')
</pre>
</div></div>
<h1><a name="JSONFunctions-ConversiontoJSON"></a>Conversion to JSON</h1>

<p>A straight-forward specification compliant conversion is used for converting values into their appropriate JSON document form.  </p>

<ul>
        <li>null values are included as the null literal.</li>
</ul>


<ul>
        <li>values parsed as JSON or returned from a JSON construction function (JSONPARSE, JSONARRAY, JSONARRAY_AGG) will be directly appended into a JSON result.</li>
</ul>


<ul>
        <li>boolean values are included as true/false literals</li>
</ul>


<ul>
        <li>numeric values are included as their default string conversion - in some circumstances if not a number or +-infinity results are allowed, invalid json may be obtained.</li>
</ul>


<ul>
        <li>string values are included in their escaped/quoted form.</li>
</ul>


<ul>
        <li>binary values are not implicitly convertable to JSON values and require a specific prior to inclusion in JSON.</li>
</ul>


<ul>
        <li>all other values will be included as their string conversion in the appropriate escaped/quoted form.</li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://docs.jboss.org/author/users/removespacenotification.action?spaceKey=TEIID">Stop watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://docs.jboss.org/author/users/editmyemailsettings.action">Change email notification preferences</a>
</div>
        <a href="https://docs.jboss.org/author/display/TEIID/JSON+Functions">View Online</a>
        |
        <a href="https://docs.jboss.org/author/pages/diffpagesbyversion.action?pageId=61505556&revisedVersion=3&originalVersion=2">View Changes</a>
                |
        <a href="https://docs.jboss.org/author/display/TEIID/JSON+Functions?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>