Determine if a JSON Property is a List

For a JSON object, properties can be numbers, text, objects, or lists. I am working on a app/screen that can display any JSON object, so the structure will not be known in advance. In my experience, unknown/changing data structures are common.

For example say I have the following JSON object:

I am having trouble determining if a given object property is text (Name, Desc) or a list (List). I tried using the list LENGTH OF block, but it returns the same value as the text LENGTH OF block.

I started playing around with various text-parsing strategies, but as many E.T.L. programmers will attest, these strategies are brittle.

Does anyone know how to determine if a JSON property is text or list (or object) programatically without parsing the results of GENERATE JSON FROM OBJECT block?

Thanks,

Ted

Lists are typically identified by square brackets [ ]. Thunkable’s list blocks do not use square brackets for some reason. They also do not use speech marks to define a string in a list.

True, JSON lists are bound by , JSON Objects by {}, and text by “”. I’m trying to avoid parsing text if possible.

As far as parsing goes, here is what I have. Again, I don’t trust parsing text, but I might have no other choice.

There is a block to get object properties list: “get object properties of__”. Essentially after converting from Json to object, use this block to get the top level list of properties, then use those to point to the object. if there are more level down, can use it again at that level, and so on. Then you design how you want to display it on the screen.

Thanks for the suggestion. I do know about the GET OBJECT PROPERTIES. It does retrieve a list of the NAMES of the properties, but it does not indicate what type of data is in the property (list, object, number, text). Or am I misunderstanding your instructions

There is only one way to determine the type of an object in Thunkable X: perform a specific operation for each type and check the result. Checking for [ and ] brackets is an unreliable solution, if only because the JSON format itself may contain errors. Therefore, it is most reliable to perform an operation that is specific to the specified type and make sure that it was performed successfully.

Unfortunately, the block for checking the list length and text length currently doesn’t work correctly in some situations. This means that you need to use other operations, such as trying to get the first item from value, and so on.

1 Like

What do you mean by “perform a specific operation for each type and check the result.”? My experience has been when you try to perform a specific action on the wrong type of data, Thunkable crashes (usually blank screen). What types of checks are you refering to that will not cause the app to fail?

Each data element has a specific data type. A data type is a set of values that it can accept and a set of operations that can be performed on it. Thunkable X doesn’t have a block for defining the data type, so you have to do it manually.

I think the above is clear to you and requires no explanation.

How do I determine the data type? To do this, perform the operation that is applicable to this value on a value only of this type and view the result. Your opinion that there is always a crash when trying to perform an operation on an incorrect data type is wrong. There are cases when this is true, but there are cases when it is not true at all and you can safely perform data type verification.

Many blocks for working with a list and text in Thunkable X can be used with both a list and text. They are not suitable for defining the data type. For this reason, you need to use blocks that will show exactly that this is a list and this is a text. To define a list, you need to perform two operations: add an item to the list and remove it from the list. If these operations are performed, we have a list in front of us.