Tip: Reading JSON

There have been a number of questions related to JSON recently so I thought I’d offer this simple guide to reading a block of JSON. In no particular order:

At its top level, JSON can either be an object (think Dictionary) or an array. The “{” denotes an object and the “[” means array.

An array can contain any kind of item that JSON recognizes. These are a string (surrounded by double-quotes), a number (integer or real), boolean (true or false), an object (see next item), another array, or null.

An object is given as a string key, colon, then a value. The value can be any valid JSON type (see above). Example: {"my key" : "string value", "another key" : true, "last key" : null}

In either an object or array, multiple values are separated by commas.

White space does not matter outside of a string, but case does. “true” is not the same as “TRUE”, and object keys are case-sensitive so you can have in the same object a key of “my key” and another of “MY KEY”.

When parsing the JSON in Xojo, the first character will determine if you will get back the equivalent of a Dictionary (starts with “{”) or an array ("["). When using Xojo.Data.ParseJSON, the result will either be a Xojo.Core.Dictionary or Auto().

If you have difficult JSON, a good first step is to add white space outside of quotes to make it easier to read. JSONItem (or my drop-in version, JSONItem_MTC) has a “pretty print” option that will do this for you. Otherwise, look elsewhere on the forum for code that will format it manually. Or just open it in an editor and add white space yourself.

See http://json.org for more information.

HTH.