Thinking About JSON, part 3

This a quick followup to last month’s part 2, because today I want to to dig a little deeper into JSONSetElement and take a closer look at the first argument:

2018-08-09_090804

As I wrote last time…

Part of what makes JSONSetElement so powerful is that it can be used both to create new entries, and to update existing entries. Specify a valid address, and it will either create the entry if it doesn’t already exist, or update it if it does.

Let’s talk about the update aspect first, because it’s extremely straight forward. If you have existing JSON in a variable, for example $$simpleJSON, you can update it (i.e., add new elements or change existing elements) like so:

    • JSONSetElement ( $$simpleJSON ; etc )

In other words, you use your existing JSON as the first argument.

But when it comes to creating new JSON, there are three possible first arguments to consider.

    • JSONSetElement ( “{}” ; etc )
      …instructing FileMaker to create a JSON object
    • JSONSetElement ( “[]” ; etc )
      …instructing FileMaker to create a JSON array
    • JSONSetElement ( “” ; etc )
      …trusting FileMaker to figure out which structure to create

At the risk of stating the obvious, in the first two cases we are being explicit, but in the third case we are not. (Note: If you’re unclear on the distinction between objects and arrays, this was covered in great detail in part 1.)

So, for example, we might create a JSON object like this…

2018-08-09_094932.png

…but if we screw up and use brackets for the first argument, of course it won’t work because we told FileMaker we wanted an array, but then specified named keys (which are only valid for objects).

2018-08-09_095743.png

If we want to declare a proper array, we can do so like this…

2018-08-09_100759

…but if we mistakenly specify braces instead of brackets…

2018-08-09_101224.png

…FileMaker is quite happy to create an object instead of an array, using the 0/1/2 as named keys corresponding to the three values. This doesn’t cause an error because numbers are perfectly valid keys (and you’ll note that they are surrounded by quotes in the resulting JSON, as keys always are).

So much for being explicit. Now let’s turn to implicit behavior, where we rely on FileMaker to figure things out for us. With the first argument left empty, FileMaker’s default guess is that we’re trying to create an object…

2018-08-09_102319

…even if we use syntax that common sense would suggest was probably intended to produce an array.

2018-08-09_102606.png

So if we really and truly are committed to leaving the first argument empty, can we force JSONSetElement to declare an array instead of an object? Yes, we can, by surrounding the 0/1/2 with quoted brackets like so:

2018-08-09_103751.png

In fact, we only need to do so for the first element, because that’s what FileMaker looks at to determine whether to create an object or an array.

2018-08-09_104117.png

What if we flip things around and use quoted brackets for the second and third elements, but not the first?

2018-08-09_104747.png

FileMaker balks because it wants to create an object, but object names (in this case the “[1]” and the “[2]”) cannot begin with an opening bracket.

Bottom line: if you leave the first argument empty, FileMaker will make a good faith effort to figure out what structure you are trying to create; but to avoid any possible misunderstanding, declare your intentions explicitly by specifying “{}” or “[]”.

2 thoughts on “Thinking About JSON, part 3

  1. Kevin Frank Post author

    Gary Davis writes to say:

    We use UUIDs converted into decimal format and one thing that you must do during the JSONSetElement is use JSONString or else it formats the number incorrectly.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.