<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Arcade - Validation of JSON data in string field? in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316660#M53814</link>
    <description>&lt;P&gt;Probably overly cumbersome but one idea is to count the number of left square brackets and then count the number of right square brackets and see if you get the same counts.&lt;/P&gt;&lt;P&gt;Maybe a loop using the FIND function to step through the&amp;nbsp;testFieldValue string. Something like this (I did not test this but hopefully you can get an idea).&lt;/P&gt;&lt;P&gt;function count_chars(mychar, mystring)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; &amp;nbsp;var mycount = 0;&lt;BR /&gt;&amp;nbsp; var start = 0;&lt;BR /&gt;&amp;nbsp; var length = COUNT(mystring);&lt;BR /&gt;&amp;nbsp; start = FIND(mychar, mystring, start+1);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;while(start &amp;lt; length &amp;amp;&amp;amp; start != -1)&lt;BR /&gt;&amp;nbsp; &amp;nbsp;{&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;mycount++;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; start = FIND(mychar, mystring, start+1);&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;return mycount;&lt;BR /&gt;}&lt;/P&gt;</description>
    <pubDate>Tue, 08 Aug 2023 22:01:02 GMT</pubDate>
    <dc:creator>RichardHolowczak</dc:creator>
    <dc:date>2023-08-08T22:01:02Z</dc:date>
    <item>
      <title>Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316597#M53805</link>
      <description>&lt;P&gt;I have a feature service that has a string field containing JSON values. I can easily convert those into Arcade Dictionary objects in my popups. If a record hasn't been populated, it is null, and I can easily test for that, or it will just create an empty Dictionary. For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Valid JSON passed in as serialized string
var testFieldValue = `{"Bart":"Simpson","foo":["a","b"]}`
var testDict = Dictionary(testFieldValue)
Console(`TypeOf: ${TypeOf(testDict)}; value: ${testDict}`)
// OUTPUT: TypeOf: Dictionary; value: {"Bart":"Simpson","foo":["a","b"]}

// Null value
testFieldValue = null
testDict = Dictionary(testFieldValue)
Console(`TypeOf: ${TypeOf(testDict)}; value: ${testDict}`)
// OUTPUT: TypeOf: Dictionary; value: {}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because the field is a string field, it should always contain a string or a null, but what if the string isn't valid JSON? Even just an empty string blows this up:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;// Garbage data - how do I test for this? Try/catch? 
testFieldValue = ""
testDict = Dictionary(testFieldValue)
Console(`TypeOf: ${TypeOf(testDict)}; value: ${testDict}`)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That raises this error in my console. If I expand the drop-downs, there are lots of lines of stack trace, but nothing I can act on.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dcafdg_0-1691522269550.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/77594i60551F6E1F742CD4/image-size/large?v=v2&amp;amp;px=999" role="button" title="dcafdg_0-1691522269550.png" alt="dcafdg_0-1691522269550.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;This is a trivial example, but imagine the field contains improperly formatted JSON data (e.g.: missing a closing quote or bracket). I'd get the same result.&lt;/P&gt;&lt;P&gt;Is there a way to validate the text in a field before running it through the &lt;FONT face="courier new,courier" color="#993300"&gt;Dictionary()&lt;/FONT&gt; function? How about some way to handle the error, like a try/catch statement? The code above crashes my popup and nothing displays, so it's real deal-breaker if my Feature Service has bad data.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 19:53:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316597#M53805</guid>
      <dc:creator>davedoesgis</dc:creator>
      <dc:date>2023-08-08T19:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316619#M53807</link>
      <description>&lt;P&gt;Can you test with IsEmpty?&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/arcade/function-reference/logical_functions/#isempty" target="_blank"&gt;https://developers.arcgis.com/arcade/function-reference/logical_functions/#isempty&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 20:38:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316619#M53807</guid>
      <dc:creator>RichardHolowczak</dc:creator>
      <dc:date>2023-08-08T20:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316620#M53808</link>
      <description>&lt;P&gt;Or replace with a Default Value&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/arcade/function-reference/logical_functions/#defaultvalue" target="_blank"&gt;https://developers.arcgis.com/arcade/function-reference/logical_functions/#defaultvalue&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 20:40:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316620#M53808</guid>
      <dc:creator>RichardHolowczak</dc:creator>
      <dc:date>2023-08-08T20:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316628#M53809</link>
      <description>&lt;P&gt;There's no try/catch available right now, but those are in the &lt;A href="https://developers.arcgis.com/arcade/guide/reserved/#future-keywords" target="_self"&gt;future keywords&lt;/A&gt; list of reserved words, so hopefully that will be implemented in a coming release.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 21:07:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316628#M53809</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-08-08T21:07:14Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316632#M53810</link>
      <description>&lt;P&gt;Yes, if it is just an empty string, I can test for that and handle it. My bigger concern is something like a JSON object that has a small error in it, like this, which is missing a closing square bracket on the array.&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;testFieldValue = `{"a": "this is a long, invalid json object", 
                   "b": ["no", "closing", "bracket"}`
testDict = Dictionary(testFieldValue)
Console(`TypeOf: ${TypeOf(testDict)}; value: ${testDict}`)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I could also see garbage non-JSON strings creeping into the data at some point.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I test for these cases and/or catch the error before it crashes my popup?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 21:19:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316632#M53810</guid>
      <dc:creator>davedoesgis</dc:creator>
      <dc:date>2023-08-08T21:19:18Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316660#M53814</link>
      <description>&lt;P&gt;Probably overly cumbersome but one idea is to count the number of left square brackets and then count the number of right square brackets and see if you get the same counts.&lt;/P&gt;&lt;P&gt;Maybe a loop using the FIND function to step through the&amp;nbsp;testFieldValue string. Something like this (I did not test this but hopefully you can get an idea).&lt;/P&gt;&lt;P&gt;function count_chars(mychar, mystring)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; &amp;nbsp;var mycount = 0;&lt;BR /&gt;&amp;nbsp; var start = 0;&lt;BR /&gt;&amp;nbsp; var length = COUNT(mystring);&lt;BR /&gt;&amp;nbsp; start = FIND(mychar, mystring, start+1);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;while(start &amp;lt; length &amp;amp;&amp;amp; start != -1)&lt;BR /&gt;&amp;nbsp; &amp;nbsp;{&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;mycount++;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; start = FIND(mychar, mystring, start+1);&lt;BR /&gt;&amp;nbsp; }&lt;BR /&gt;return mycount;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 22:01:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316660#M53814</guid>
      <dc:creator>RichardHolowczak</dc:creator>
      <dc:date>2023-08-08T22:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316664#M53815</link>
      <description>&lt;P&gt;Ha, I thought about this, too! There's just so many ways this could go sideways. It seems simple enough on the surface,&amp;nbsp; but feels like a task bound to snowball in complexity. As far as I can tell, there's no way to write a reusable library, so it seems like a lot to add JSON validation just to produce a popup.&lt;/P&gt;&lt;P&gt;Note that I also tried the &lt;FONT face="courier new,courier" color="#993300"&gt;FromJSON()&lt;/FONT&gt; function. I was hoping that serialized JSON with errors would just result in a String variable, but this also produced an error similar to the &lt;FONT face="courier new,courier" color="#993300"&gt;Dictionary() &lt;/FONT&gt;function.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 22:11:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316664#M53815</guid>
      <dc:creator>davedoesgis</dc:creator>
      <dc:date>2023-08-08T22:11:47Z</dc:date>
    </item>
    <item>
      <title>Re: Arcade - Validation of JSON data in string field?</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316667#M53816</link>
      <description>&lt;P&gt;For sure there are lots of ways an input doc. can be mis-formatted. There were posts going back 5 or 6 years asking for a try..catch.&amp;nbsp; &amp;nbsp;Hopefully something like that will be available soon.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Aug 2023 22:17:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/arcade-validation-of-json-data-in-string-field/m-p/1316667#M53816</guid>
      <dc:creator>RichardHolowczak</dc:creator>
      <dc:date>2023-08-08T22:17:50Z</dc:date>
    </item>
  </channel>
</rss>

