Dealing with missing Json record

When making a call to the AirTable api if a field doesnt have any data for a particular record the api wont send the field at all.
for example if I have a ITEM field but for a record that field is empty then when I attempt to set the data of that field to a variable it fails.
What is the best strategy for this? I thought to have if statement for each field that tests if it exists or if the record contains each field I need to set to a variable but that seems to not be that reliable?

If you’re dealing with an AppleScript record, and you want to read the value of a property that may or may not exist, e.g.:

set var to b of {a: "one", c: pi, d: "IV"}

then leverage the fact that, when two records are combined using the & operator:

{a: 1, b: 1} & {a: 2, c: 2}

will yield

{a: 1, b: 1, c: 2}

Thus, you can effectively set a “default” value for any property that might not exist:

set var to b of ({a: "one", c: pi, d: "IV"} & {b: missing value, d: 4.0})

If property d did not exist in the leftmost record, it would take on the value 4.0 when assigned to property d in var. However, it does exist in the leftmost record, so d in var will have the value of "IV", whereas b in var will be assigned the “default” value from the rightmost record, namely missing value. So, var will end up being a record with the following properties:

{a: "one", b: missing value, c: 3.141592654, d: "IV"}
1 Like

Hey @CJK,

Thanks – I’d forgotten about that little gem.

Is it working for you verbatim? If so on what system?

On Mojave I required a little mod:

set var to b of ({a:"one", c:pi, d:"IV"} & {b:missing value, d:4.0})


1 Like

Oops! Thank you. I’ll make the edit.

[ I don’t have a working Mac right now, which isn’t an excuse in this particular case. ]

1 Like

Ahh this is awesome so if the first array is the returned json and I append that array with the & operator and an empty array
`set theJson to {a:“one”, c:pi, d:“IV”}

set var to a of (theJson & {a:"", b:"", c:"", d:""})`

Yes, that’s correct, if by array, you meant record. In your example, the value assigned to var will be the value of property a from your JSON record, since it it present. In situations where it was absent, it would be assigned the value "" that you’ve supplied via your second record.