Getting the correct object array from an API JSON input

Hey, I am trying to get the vehicle registration from my API. I have output the response and found that the response is correct, but when I try to get the information it seems the vehicle registration list I have created is 0 (as shown in the screenshot below).

I am assuming my formatting is incorrect, but am hoping for some assistance here.

I ahve been getting a whole bunch of similar information in other screens from these endpoints, and fonud this block structure worked perfectly, but this time it didn’t wrk and I am stumped. Sorry about the dumb question, I am still a Thunkable beginner. I fonud the length of the object list is 0, but the response is correct, so I am not sure what I am doing wrong.

Here are mu blocks

Here is a swagger screenshot of what the format looks like.
image

Here are my screenshots of the two stages at the screen.


Here is my JSON output from Postman. I think the issue is the vehicle rego detials are nested twice and I am unsure of how to go about extracting the info correctly. Any suggestions are greatly appreciated. Cheers

{
    "user_details": {
        "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
        "name": "Luke",
        "mobileNumber": "+61413555236",
        "email": "luke5@gmail.com"
    },
    "vehicle_reg": {
        "123456": {
            "model": "234566",
            "colour": "098765",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "123456",
            "make": "123456"
        },
        "123ABC": {
            "model": "Vroom vroom",
            "colour": "Pink",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "123ABC",
            "make": "Toyota"
        },
        "AAAAA": {
            "model": "Vroom vroom",
            "colour": "Pink",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "AAAAA",
            "make": "Toyota"
        },
        "ABN456": {
            "model": "hiace",
            "colour": "red",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "ABN456",
            "make": "toyota"
        },
        "TESTLPPP": {
            "model": "hiaceyyyy",
            "colour": "aquamarine",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "TESTLPPP",
            "make": "toyoto"
        }
    }
}

In your swagger screenshot, vehicle_reg is an array/list but in your Thunkable screenshot, it’s not. So you can’t loop through vehicle_reg as you’re doing in your blocks.

I think you have to do something like get the properties of the vehicle_reg object and then loop through that list of properties.

1 Like

I kinda get what you’re saying, but I am not sure how to implement that. Also just wanted to say thanks, I’ve noticed you are jumping on a few of my forum posts and I definitely appreciate it!

I have managed to extract the data when I know exactly what it is. See the below screenshot.

But what I don’t get how to implement is how to extract it without knowing what the value (rego 123ABC as an example) is. From what you’ve said, I believe I have done this, but when I attempt to extract it, I get a length of 0 for the vehicle_reg_list and am unsure of why I am gettnig the vehicle reg info incorrectly.

1 Like

You’re welcome! Happy to help when I can…

Here’s how I would extract the “lpn” property:

And the preview:

FYI, you can simplify the property names to “vehicle_reg.” + j + “.lpn”. I just made them that way because the screenshot is a little easier to understand.

1 Like

Thanks for doing up that example, but I am a bit confused by it and hoping for some clarification.

I truthfully don’t understand quite a few things you’ve done, but ended up doing this and was able to output them to the listviewer and actually populate it. It was simply adding the “get object prooperties” bit before the other section I already had. Just trying to now figure out the bit about extracting the lpn and make info for each vehicle.

Can you explain what the JSON response is? As I am not sure exactly what that is and where it came from. I know you’ve set it at teh top, but that user information will be different for every user, so I am not sure if that would work in this case?

I have attempted this but it doesn’t seem to extract the correct information, just wondering if you knew what was going wrong with this setup. I tried doing it with your example, but I got fairly confused and wanted to try it a different way.

Sorry, I meant to explain that the JSON response is just a static value for the JSON you posted above. Instead of calling the server, I just used your response. It has the same effect so it’s useful for testing the blocks, especially for APIs where I wouldn’t have a key to access them.

But you’re right, the JSON is going to be dynamic (different every time). If the format of the JSON is the same but the values changes, then you’re fine using the blocks I provided. You just wouldn’t need the static JSON text. You’d replace that with the actual JSON response (green [response] block).

If the format changes, then you are in for quite a ride… and I’d need to see some examples of what that looks like in order to advise you.

The main problem with your blocks is that you are assuming that the “vehicle_reg” property you’re getting from your API is an array/list. It isn’t. Square brackets in JSON [ ] represent an array. Curvy brackets { } represent objects.

A list of objects would look like this:

"vehicle_reg": [
        "123456": {
            "model": "234566",
            "colour": "098765",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "123456",
            "make": "123456"
        },
        "123ABC": {
            "model": "Vroom vroom",
            "colour": "Pink",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "123ABC",
            "make": "Toyota"
        }
]

But what you’re seeing is an object with objects inside:

"vehicle_reg": {
        "123456": {
            "model": "234566",
            "colour": "098765",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "123456",
            "make": "123456"
        },
        "123ABC": {
            "model": "Vroom vroom",
            "colour": "Pink",
            "cognito_id": "5820d31f-8d80-41c2-8372-f5aade306d20",
            "lpn": "123ABC",
            "make": "Toyota"
        }
}

So you’re stuck using “get property of object” blocks. What I did to make the blocks access multiple nested properties/objects is to use the “get object properties of” block. That’s a special block that is used to get the property names from inside of an object. I assign that to the loop variable j and then use j in place of the actual property name such as “123ABC” or “AAAA”.

I’m sure that’s still a little confusing but I’m not sure how else to explain it!

Ah, I get what you mean. That makes a lot of sense, I believe I have implemented it correctly (and yes the format stays the same, but the values change). But I am not getting the correct output, pelase see the screenshos below. When I try to get the lpn values and output them to the listviewer, it doesn’t work.

Because the API response is in JSON format, wherever you have a green [response] block, you need a “get object from JSON” block before it. You’re missing that here:

Also, “j” is not an object. Using my blocks, you’re now getting a list of text strings stored in app variable vehicle_reg_list. So you can’t use that as an object when trying to get the “make” property.

Instead, copy the [in list…] set of blocks you’re using to get the “lpn” property and just change “lpn” to “make” here:

The structure of those blocks will work for any nested property such as “model”, “colour”, etc.

The idea is that as “j” loops through the values in the list, it joins together the values to create strings like this in the get property of object block inside your loop:

“vehicle_reg.123456.lpn”
“vehicle_reg.123ABC.lpn”
“vehicle_reg.AAAAA.lpn”

To get the “make” properties, you’d want to do the same but replace “lpn” with “make”:

“vehicle_reg.123456.make”
“vehicle_reg.123ABC.make”
“vehicle_reg.AAAAA.make”

*The values in bold above are your “j” values in the loop.

I gotta get some sleep but hopefully that will get you far enough along to retrieve some of the values you need…