JSON / Object Tutorial

Part 2 JSON Errors

Un-Thunkable
In a perfect world, everyone would have everything they need for an abundant life. Guess what, we don’t live in a perfect world. :poop: happens. When it does, Thunkable misbehaves :skull_and_crossbones:. But these misbehaviors are predictable. This section will cover the most common errors and misbehaviors when working with JSON in Thunkable. When you understand the fundamentals of JSON and how Thunkable misbehaves, you will be able to identify 90% of all JSON-related errors immediately.

null Object
null is a little tricky to describe. It is not the word “null”. It is not a blank or empty value. It is simply nothingness!

Thunkable does NOT like null. Thunkable will crash. If we create a variable, set it to null, then try to treat it as an object, the thunkable app will crash.

image

When using the Web Live Test, the screen simply goes blank (white). When using the android live test, you will receive a message:

Likewise, trying to set a property of a null will cause Thunkable to crash:
image

The simpliest way to fix this problem is to first check to make sure the object is not null.
image
And don’t worry about this affecting the end user experience. If you app is working correctly, they will never see this message. :thinking:

null Value
What if an object has been defined, the name has been defined, but the value is null?
image
This is where things get a little wierd. If the value is set to null and you display it in a label, it will show null. That looks like the value is “null”.
image
But let’s take a closer look by asking thunkable to generate the JSON from the object


image
Notice anything strange? alias is surrounded by quotes, but null is not. Remember that JSON values containing strings are enclosed by quotation marks?

Once again, null is not the letters n-u-l-l, but a special case. Hence, it is stored in JSON as if it were a number. Weird, but true. :confounded:

Here is a straightforward illustration of the point that a null value without quotes is not a word, but a special value.
image
image

The key lesson here is that even if the label shows the word null, you need to check for the special value of null (found in the logic blocks).

Text is not JSON
Working with external services (Web APIs, data sources, web viewers, etc.) often share date using JSON. As discussed previously, the get object from JSON block can convert text to a Thunkable object. But what if the JSON is not propertly formatted?

In the example below, the text is missing the final }


Thunkable simply crashes when this is attempted. Web Apps just go blank, device Live Test have an error message we have seen before.
image

To keep the app from crashing, there are a few simple checks you can make:

  1. Trim off leading and trailing spaces
  2. Check that the first character is a { and the last character is a }
  3. If the string checks out, use it.
  4. If the string doesn’t check out, show a warning message

Variable not object
Despite our best efforts, occasionally a variable is set to an unexpected value. This can often occur when you accidentally set the variable to the JSON string, not a JSON object.
image
This will return the result undefined.

If you were paying attention to the null discussion, you might expect undefined to be something other than the string undefined. Well you would be right. Unfortunately undefined is not a string and there is no special block called undefined. But all hope is not lost! You can create a check that Thunkable will recognize by comparing the expected value to a known undefined value.


The good new is that you don’t actually need to do this. This type of error usually only occurs during development. As soon as you display the variable and see undefined, you now know what one of the sources is. Another source is described below.

Property does not exist
Similarly, if the variable is an object, but the property doesn’t exist, you will also get a result of undefined.
image

Strategies to prevent this from occuring are discussed in the Intermediate JSON and Advanced JSON discussions below.

[object Object]
Just as Thunkers sometimes treat JSON text as a Thunkable objects, if you are anything like me, you will eventually treat an object like text. Luckily, setting a label to an object creates a very distinctive message.
image

image

Summary
If you survived this TL;DR post, you now can troubleshoot the following errors when working with JSON:

  1. Thunkable crashes
  2. Thunkable displays undefined
  3. Thunkable displays [object Object]
  4. Thunkable displays null

Congratulations!, looking for the 4 situations will resolve 19 out of 20 JSON related problems. the remaining issues will be addressed in the sections below.

4 Likes