Site icon FileMakerHacks

Thinking About JSON, part 3

This article is part of a series. See also…
•  Thinking About JSON, part 1
•  Thinking About JSON, part 2
•  Thinking About JSON, part 4

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:

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:

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.

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…

…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).

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

…but if we mistakenly specify braces instead of brackets…

…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…

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

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:

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.

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

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 “[]”.

Exit mobile version