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:
// 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: {}
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:
// Garbage data - how do I test for this? Try/catch?
testFieldValue = ""
testDict = Dictionary(testFieldValue)
Console(`TypeOf: ${TypeOf(testDict)}; value: ${testDict}`)
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.
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.
Is there a way to validate the text in a field before running it through the Dictionary() 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.