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. happens. When it does, Thunkable misbehaves . 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.
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:
The simpliest way to fix this problem is to first check to make sure the object is not null.
And don’t worry about this affecting the end user experience. If you app is working correctly, they will never see this message.
null Value
What if an object has been defined, the name has been defined, but the value is null?
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”.
But let’s take a closer look by asking thunkable to generate the JSON from the object
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.
Here is a straightforward illustration of the point that a null value without quotes is not a word, but a special value.
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.
To keep the app from crashing, there are a few simple checks you can make:
- Trim off leading and trailing spaces
- Check that the first character is a
{
and the last character is a}
- If the string checks out, use it.
- 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.
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.
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.
Summary
If you survived this TL;DR post, you now can troubleshoot the following errors when working with JSON:
- Thunkable crashes
- Thunkable displays undefined
- Thunkable displays [object Object]
- 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.