Working with %JSON.Adaptor
Tags: json, programming, tutorial, productivity
Published: 2024-01-23 19:13:43

<p style="margin-bottom:13px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">In this article,&nbsp;&nbsp;I walk through the&nbsp;</span></span></span><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;"><a href="https://docs.intersystems.com/irislatest/csp/documatic/%25CSP.Documatic.cls?LIBRARY=%25SYS&amp;CLASSNAME=%25JSON.Adaptor" target="_blank">%JSON.Adaptor</a> class. This class is extremely useful in exporting data into serialized JSON and importing the JSON objects and saves into class object.</span></span></span></p>
<p style="margin-bottom:13px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">I'll start with the %JSON. Adaptor parameters. The parameters are crucial part of the JSON import and export operations.</span></span></span></p>
<p style="margin-left:48px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">If your property name is not the same as the actual JSON key name for export to external or load the data’s into object through import, you should use the<span style="color:#3498db;"> <strong>%JSONFIELDNAME</strong></span> parameter to define the alias name. If it doesn't, it produces an 'unexpected field' in the input error at the time of import.</span></span></span></p>

<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">Class</span> Samples.TestJSONAdaptor <span class="hljs-keyword">Extends</span> (<span class="hljs-built_in">%Persistent</span>, <span class="hljs-built_in">%JSON.Adaptor</span>)
{
    <span class="hljs-keyword">Property</span> Name <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONFIELDNAME</span> = <span class="hljs-string">"First_Name"</span>)<span class="hljs-comment">;</span>
<span class="hljs-keyword">ClassMethod</span> ExportToJSON()
{
    <span class="hljs-keyword">Set</span> obj = <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).<span class="hljs-built_in">%OpenId</span>(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExport</span>() 
}
}
Output
LEARNING&gt;<span class="hljs-keyword">Do</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).ExportToJSON()
{<span class="hljs-string">"First_Name"</span>:<span class="hljs-string">"myFirstName"</span>}</code></pre>
<p style="margin-left:48px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">In certain situations, you may not want to expose or skip certain properties and internal keyword defined properties (Note: Property will expose in JSON even though if it’s defined as an internal keyword) in the JSON output. In order to prevent the field from being exposed to the outside world, you could mention this parameter&nbsp;<strong><span style="color:#3498db;">%JSONINCLUDE</span></strong></span></span></span><br><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">. Default value is INOUT</span></span></span></p>

<ol>
<li style="list-style-type:none">
<ol style="list-style-type:lower-alpha">
<li style="margin-left:16px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;"><strong>INPUTONLY </strong>- It will only accept input and not export data to the outside world.</span></span></span></li>
<li style="margin-left:16px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;"><strong>OUTPUTONLY </strong>- It shows the values in the outside world. However, Null values can only be stored if this parameter was mentioned to this property. Be cautious when utilizing this parameter.</span></span></span></li>
<li style="margin-left:16px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;"><strong>NONE </strong>- JSON output or input should never be stored and retrieved.</span></span></span></li>
</ol></li>
</ol>
<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">Class</span> Samples.TestJSONAdaptor <span class="hljs-keyword">Extends</span> (<span class="hljs-built_in">%Persistent</span>, <span class="hljs-built_in">%JSON.Adaptor</span>)
{
    <span class="hljs-keyword">Property</span> Name <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONFIELDNAME</span> = <span class="hljs-string">"First_Name"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> Phone <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Integer</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"INPUTONLY"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> IsActive <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Boolean</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"OUTPUTONLY"</span>)<span class="hljs-comment">;</span>
<span class="hljs-keyword">ClassMethod</span> ExportToJSON()
{
    #<span class="hljs-comment">; JSON Export ouput</span>
    <span class="hljs-keyword">Set</span> obj = <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).<span class="hljs-built_in">%OpenId</span>(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExport</span>() 
    #<span class="hljs-comment">; output &gt;&gt; {"First_Name":"myFirstName","IsActive":true}</span>
    #<span class="hljs-comment">; Object ouput</span>
    <span class="hljs-keyword">Write</span> obj.Phone 
    #<span class="hljs-comment">; output &gt;&gt; 9639639636</span>
}

Output
LEARNING&gt;<span class="hljs-keyword">Do</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).ExportToJSON()
 {<span class="hljs-string">"First_Name"</span>:<span class="hljs-string">"myFirstName"</span>,<span class="hljs-string">"IsActive"</span>:true} 
<span class="hljs-number">9639639636</span></code></pre>
<p style="margin-left:48px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">Generally, the instance doesn't have any values in the properties that are not part of the JSON export. In order to include those properties in JSON export, it is necessary to include the <strong><span style="color:#3498db;">%JSONNULL</span></strong> parameter. The output will have the default value of null. Furthermore, you could define this parameter option globally within your specific class definition instead of a specific field. &nbsp;This will be applicable to all fields.</span></span></span></p>

<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">Class</span> Samples.TestJSONAdaptor <span class="hljs-keyword">Extends</span> (<span class="hljs-built_in">%Persistent</span>, <span class="hljs-built_in">%JSON.Adaptor</span>)
{

    <span class="hljs-keyword">Property</span> Name <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONFIELDNAME</span> = <span class="hljs-string">"First_Name"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> Phone <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Integer</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"INPUTONLY"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> IsActive <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Boolean</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"OUTPUTONLY"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-comment">/// This field is available in JSON export</span>
    <span class="hljs-keyword">Property</span> JSONNULLDeclared <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONNULL</span> = <span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
    <span class="hljs-comment">/// This field is not available in JSON export</span>
    <span class="hljs-keyword">Property</span> JSONNULLNotDeclared <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span><span class="hljs-comment">;</span>
<span class="hljs-keyword">ClassMethod</span> ExportToJSON()
{
    #<span class="hljs-comment">; JSON Export </span>
    #<span class="hljs-comment">; JSONNULLDeclared and JSONNULLDeclared - values are empty in object</span>
    <span class="hljs-keyword">Set</span> obj = <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).<span class="hljs-built_in">%OpenId</span>(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExport</span>() 
    #<span class="hljs-comment">; output &gt;&gt; {"First_Name":myFirstName"","IsActive":true,"JSONNULLDeclared":null}</span>
}
}
Output
LEARNING&gt;<span class="hljs-keyword">Do</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).ExportToJSON()
{<span class="hljs-string">"First_Name"</span>:myFirstName<span class="hljs-string">""</span>,<span class="hljs-string">"IsActive"</span>:true,<span class="hljs-string">"JSONNULLDeclared"</span>:null}
</code></pre>
<p style="margin-left:48px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">&nbsp;Basically, empty strings in the JSON input are stored as $c(0). If there are objects script "" values in the property, they won't be displayed in the JSON because %JSONIGNORENULL. Verify that the empty string conditions are met and export the data by enabling the %JSONIGNORENULL = 1. Furthermore, you could define and utilize this parameter option globally.</span></span></span></p>

<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">Class</span> Samples.TestJSONAdaptor <span class="hljs-keyword">Extends</span> (<span class="hljs-built_in">%Persistent</span>, <span class="hljs-built_in">%JSON.Adaptor</span>)
{

    <span class="hljs-keyword">Parameter</span> <span class="hljs-built_in">%JSONNULL</span> = <span class="hljs-number">1</span><span class="hljs-comment">; ///declared globally</span>
    <span class="hljs-keyword">Property</span> Name <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONFIELDNAME</span> = <span class="hljs-string">"First_Name"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> Phone <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Integer</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"INPUTONLY"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> IsActive <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Boolean</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"OUTPUTONLY"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> JSONNULLDeclared <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONNULL</span> = <span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> JSONNULLNotDeclared <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONIGNORENULL</span> = <span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
<span class="hljs-keyword">ClassMethod</span> ExportToJSON()
{
    <span class="hljs-keyword">Set</span> obj = <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).<span class="hljs-built_in">%OpenId</span>(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExport</span>() 
}
}
Output
LEARNING&gt;<span class="hljs-keyword">Do</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).ExportToJSON()
{<span class="hljs-string">"First_Name"</span>:<span class="hljs-string">"myFirstName"</span>,<span class="hljs-string">"IsActive"</span>:true,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLNotDeclared"</span>:<span class="hljs-string">""</span>}</code></pre>
<p style="margin-left:48px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">A lot of reference persistent or serial classes are linked in a class definition. So if you're exporting the class into JSON, it exports all the reference class fields as well into the JSON. If you prefer to export only ids instead of the entire value, you can use <strong><span style="color:#3498db;">%JSONREFERENCE</span>.</strong></span></span></span></p>

<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">Class</span> Samples.TestJSONAdaptor <span class="hljs-keyword">Extends</span> (<span class="hljs-built_in">%Persistent</span>, <span class="hljs-built_in">%JSON.Adaptor</span>)
{

    <span class="hljs-keyword">Parameter</span> <span class="hljs-built_in">%JSONNULL</span> = <span class="hljs-number">1</span><span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> Name <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONFIELDNAME</span> = <span class="hljs-string">"First_Name"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> Phone <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Integer</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"INPUTONLY"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> IsActive <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Boolean</span>(<span class="hljs-built_in">%JSONINCLUDE</span> = <span class="hljs-string">"OUTPUTONLY"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> JSONNULLDeclared <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONNULL</span> = <span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> JSONNULLNotDeclared <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONIGNORENULL</span> = <span class="hljs-number">1</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> SubClass <span class="hljs-keyword">As</span> Samples.TestJSONAdaptor.SubClass(<span class="hljs-built_in">%JSONREFERENCE</span> = <span class="hljs-string">"ID"</span>)<span class="hljs-comment">;</span>
<span class="hljs-keyword">ClassMethod</span> ExportToJSON()
{
    #<span class="hljs-comment">; JSON Export </span>
    <span class="hljs-keyword">Set</span> obj = <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).<span class="hljs-built_in">%OpenId</span>(<span class="hljs-number">1</span>)
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExport</span>() 
}
}

output
LEARNING&gt;<span class="hljs-keyword">d</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).ExportToJSON()
   {<span class="hljs-string">"First_Name"</span>:<span class="hljs-string">"Ashok"</span>,<span class="hljs-string">"IsActive"</span>:true,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLNotDeclared"</span>:<span class="hljs-string">""</span>,<span class="hljs-string">"SubClass"</span>:{<span class="hljs-string">"RefId"</span>:<span class="hljs-string">"a3295"</span>,<span class="hljs-string">"TestProperty"</span>:<span class="hljs-string">"Test1"</span>}} <span class="hljs-comment">; %JSONREFERENCE = "OBJECT"</span>
LEARNING&gt;<span class="hljs-keyword">d</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).ExportToJSON()
    {<span class="hljs-string">"First_Name"</span>:<span class="hljs-string">"myFirstName"</span>,<span class="hljs-string">"IsActive"</span>:true,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLNotDeclared"</span>:<span class="hljs-string">""</span>,<span class="hljs-string">"SubClass"</span>:<span class="hljs-string">"a321"</span>} <span class="hljs-comment">; %JSONREFERENCE = "ID"</span>
</code></pre>
<p style="margin-left:48px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">One of the most crucial parameters. When you load the JSON object into a class object, there may be a change that causes unexpected fields to be added to the JSON. An error is thrown if the parameter <strong><span style="color:#3498db;">%JSONIGNOREINVALIDFIELD</span></strong> is not added. Enabling this parameter in your class definition is highly beneficial.</span></span></span></p>

<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-comment">///Before enabling %JSONIGNOREINVALIDFIELD</span>
<span class="hljs-keyword">Class</span> Samples.TestJSONAdaptor1 <span class="hljs-keyword">Extends</span> (<span class="hljs-built_in">%Persistent</span>, <span class="hljs-built_in">%JSON.Adaptor</span>)
{
    <span class="hljs-keyword">Property</span> Name <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONFIELDNAME</span> = <span class="hljs-string">"First_Name"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> IsActive <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Boolean</span><span class="hljs-comment">;</span>
<span class="hljs-keyword">ClassMethod</span> TestJSONImport()
{
    <span class="hljs-keyword">Set</span> json = {<span class="hljs-string">"Name"</span>:<span class="hljs-string">"MytestNewName"</span>,<span class="hljs-string">"IsActive"</span>:true}
    <span class="hljs-keyword">Set</span> obj = ..<span class="hljs-built_in">%New</span>()
    <span class="hljs-keyword">Set</span> st = obj.<span class="hljs-built_in">%JSONImport</span>(json)
    <span class="hljs-keyword">If</span> <span class="hljs-built_in">$$$ISERR</span>(st) <span class="hljs-keyword">w</span> <span class="hljs-built_in">$SYSTEM</span>.OBJ.DisplayError(st)
    <span class="hljs-keyword">Write</span> obj.<span class="hljs-built_in">%Save</span>()
}
}
output
LEARNING&gt;<span class="hljs-keyword">Do</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor1).TestJSONImport()
 
ERROR #<span class="hljs-number">9404</span>: Unexpected field in input, Name, using <span class="hljs-keyword">class</span> base mapping.11{<span class="hljs-string">"First_Name"</span>:null}
LEARNING&gt;

<span class="hljs-comment">///After enabling %JSONIGNOREINVALIDFIELD</span>
<span class="hljs-keyword">Class</span> Samples.TestJSONAdaptor1 <span class="hljs-keyword">Extends</span> (<span class="hljs-built_in">%Persistent</span>, <span class="hljs-built_in">%JSON.Adaptor</span>)
{
    <span class="hljs-keyword">Parameter</span> <span class="hljs-built_in">%JSONIGNOREINVALIDFIELD</span> <span class="hljs-keyword">As</span> BOOLEAN = <span class="hljs-number">1</span><span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> Name <span class="hljs-keyword">As</span> <span class="hljs-built_in">%String</span>(<span class="hljs-built_in">%JSONFIELDNAME</span> = <span class="hljs-string">"First_Name"</span>)<span class="hljs-comment">;</span>
    <span class="hljs-keyword">Property</span> IsActive <span class="hljs-keyword">As</span> <span class="hljs-built_in">%Boolean</span><span class="hljs-comment">;</span>
<span class="hljs-keyword">ClassMethod</span> TestJSONImport()
{
    <span class="hljs-keyword">Set</span> json = {<span class="hljs-string">"Name"</span>:<span class="hljs-string">"MytestNewName"</span>, <span class="hljs-string">"Phone"</span>:<span class="hljs-number">9639631236</span>, <span class="hljs-string">"IsActive"</span>:true, <span class="hljs-string">"unspecified"</span>:<span class="hljs-string">""</span> }
    <span class="hljs-keyword">Set</span> obj = ..<span class="hljs-built_in">%New</span>()
    <span class="hljs-keyword">Set</span> st = obj.<span class="hljs-built_in">%JSONImport</span>(json)
    <span class="hljs-keyword">If</span> <span class="hljs-built_in">$$$ISERR</span>(st) <span class="hljs-keyword">w</span> <span class="hljs-built_in">$SYSTEM</span>.OBJ.DisplayError(st)
    <span class="hljs-keyword">Write</span> <span class="hljs-string">"Save : "</span>,obj.<span class="hljs-built_in">%Save</span>(),!
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExport</span>()
}
}
LEARNING&gt;<span class="hljs-keyword">Do</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).TestJSONImport()
Save : <span class="hljs-number">1</span>
{<span class="hljs-string">"First_Name"</span>:null,<span class="hljs-string">"IsActive"</span>:null,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLNotDeclared"</span>:<span class="hljs-string">""</span>,<span class="hljs-string">"SubClass"</span>:null,<span class="hljs-string">"GblCharStrm"</span>:null,<span class="hljs-string">"GblBinStrm"</span>:null,<span class="hljs-string">"Decimal"</span>:null}
 </code></pre>
<p style="margin-bottom:13px; margin-left:48px">&nbsp;</p>
<p style="margin-bottom:13px; margin-left:48px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">In addition, Some of the rarely used parameter is '%JSONENABLED. If you don’t want to generate the JSON, Adaptor-class method generators do not produce a runnable method. Set the parameter to 0. Unless otherwise specified, the default value is 1.</span></span></span></p>

<h4 style="margin-bottom: 13px;"><u><strong><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">Native API methods for JSON adaptor</span></span></span></strong></u></h4>
<p><strong><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">%JSONImport</span></span></span></strong></p>
<p style="text-indent:36.0pt; margin-bottom:13px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">This is actual method to importing the JSON object into the current class object. This method is verifying all the properties data type with the JSON key-value data type and declare the value to the property. You can refer the above sample codes for JSON import.</span></span></span></p>

<ul>
<li><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">Remember that the JSON data type must be the same as the property data type when importing data. Otherwise it will throw and error</span></span></span></li>
<li><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">Boolean data type values 0 or 1 not same as JSON data type Boolean true or false. So, JSON must have true/false instead of 0 or 1</span></span></span></li>
<li><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">Always Use JSONFIELDNAME or XDataMapping block to avoid unexpected field mismatch error.</span></span></span></li>
<li><font face="Calibri, sans-serif"><span style="font-size: 14.6667px;">Better to use %JSONIGNOREINVALIDFIELD parameter to avoid&nbsp;</span></font><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">unexpected field error.</span></span></span></li>
</ul>
<p style="margin-bottom:13px"><strong><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">%JSONExport</span></span></span></strong></p>
<p style="text-indent:36.0pt; margin-bottom:13px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">A typical method for writing the serialized JSON to the current device. Whenever you’re exporting and importing the floating point number with trailing zeros those are not part of the JSON output. Because The ObjectScript Decimal and the IEEE Binary numeric types do not keep track of trailing zeroes.&nbsp;you can refer the above sample codes for JSON Export.</span></span></span></p>
<p style="margin-bottom:13px"><strong><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">%JSONExportToStream</span></span></span></strong></p>
<p style="text-indent:36.0pt; margin-bottom:13px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">Additional export options are available in %JSON.Adaptor. You can export your JSON as a stream object, using %JSONExportToStream.</span></span></span></p>

<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">ClassMethod</span> TestEXportToStream()
{
    <span class="hljs-keyword">Set</span> obj = <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).<span class="hljs-built_in">%OpenId</span>(<span class="hljs-number">12</span>)
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExportToStream</span>(.stream) 
    <span class="hljs-keyword">Write</span> stream,!
    <span class="hljs-keyword">If</span> <span class="hljs-built_in">$IsObject</span>(stream) {
        <span class="hljs-keyword">Write</span> stream.<span class="hljs-keyword">Read</span>(<span class="hljs-built_in">$$$MaxLocalLength</span>)
    }
    <span class="hljs-keyword">Return</span>
}

Output
EARNING&gt;<span class="hljs-keyword">d</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).TestEXportToStream()
<span class="hljs-number">3</span>@<span class="hljs-built_in">%Library.FileCharacterStream</span>
{<span class="hljs-string">"First_Name"</span>:<span class="hljs-string">"TestNameSetMethod"</span>,<span class="hljs-string">"IsActive"</span>:true,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLNotDeclared"</span>:<span class="hljs-string">""</span>,<span class="hljs-string">"SubClass"</span>:<span class="hljs-string">"a3256"</span>,<span class="hljs-string">"GblCharStrm"</span>:<span class="hljs-string">"Test string added as the global character stream"</span>,<span class="hljs-string">"Decimal"</span>:<span class="hljs-number">12</span>}</code></pre>
<p style="margin-bottom:13px"><strong><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">%JSONExportToString&nbsp;</span></span></span></strong></p>
<p style="margin-bottom:13px"><span style="font-size:11pt"><span style="line-height:115%"><span style="font-family:Calibri,&quot;sans-serif&quot;">You can export the JSON object as a string by using %JSONExportToString method.</span></span></span></p>

<pre class="codeblock-container" idlang="0" lang="ObjectScript" tabsize="4"><code class="language-cls hljs cos"><span class="hljs-keyword">ClassMethod</span> TestEXportToString()
{
    <span class="hljs-keyword">Set</span> obj = <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).<span class="hljs-built_in">%OpenId</span>(<span class="hljs-number">12</span>)
    <span class="hljs-keyword">Do</span> obj.<span class="hljs-built_in">%JSONExportToString</span>(.string) 
    <span class="hljs-keyword">Write</span> string
    <span class="hljs-keyword">Return</span>
}
output
LEARNING&gt;<span class="hljs-keyword">d</span> <span class="hljs-keyword">##class</span>(Samples.TestJSONAdaptor).TestEXportToString()
{<span class="hljs-string">"First_Name"</span>:<span class="hljs-string">"TestSetMethod"</span>,<span class="hljs-string">"IsActive"</span>:true,<span class="hljs-string">"JSONNULLDeclared"</span>:null,<span class="hljs-string">"JSONNULLNotDeclared"</span>:<span class="hljs-string">""</span>,<span class="hljs-string">"SubClass"</span>:<span class="hljs-string">"a3256"</span>,<span class="hljs-string">"GblCharStrm"</span>:<span class="hljs-string">"Test string added as the global character stream"</span>,<span class="hljs-string">"Decimal"</span>:<span class="hljs-number">12</span>}</code></pre>
<p>&nbsp;</p>