Parsing Data From an API into a List of Unknown Size

I am trying to parse information from an API and output the names of each location on the JSON into a listviewer.

I previously asked about how to correctly extract the info and have managed to do so in this thread (Trying to extract a nested list from a Web API - #8 by ankitn5 ) - thanks so much for your help so far. I figured putting this in a different thread would help others in the future when they are looking for info.

My output response from the API looks like:

{
	"locations":
	[
		{	
			"cost":1.2,
			"address":"22 Wickham Street, Wickham 2293\n(Entrance on Charles Street)",
			"max_duration":14400,
			"name":"Wickham Terraces",
			"gateway_id":"8642196e-83b5-4e3e-ad9b-60eb5c4f4174"
		},
		{
			"cost":1.2,
			"address":"Address 3",
			"max_duration":14400,
			"name":"Add 3",
			"gateway_id":"8642196e-83b5-4e3e-ad9b-60eb5c4f41755555"
		},
		{
			"cost":1.2,
			"address":"Add 4",
			"max_duration":14400,
			"name":"Add 4",
			"gateway_id":"8642196e-83b5-4e3e-ad9b-6999990eb5c4f4174"
		},
		{
			"cost":1.4,
			"address":"608 Harris Street",
			"max_duration":16400,
			"name":"UTS Tester",
			"gateway_id":"test-2"
		}
	]
}

This is an example of the type of JSON rsponse I would receive, but in future it will be x number of locations (instead of the 4 in the example JSON above) provided so I need to figure out how to parse x number of items into the listviewer.

I currently have the following setup on Thunkable. This setup extract the location information into a list of objects (as seen the the solved thread above).

And this is what my app looks like when I have run the code.

I am able to output the information into a listviewer for a defined number of locations (for example 4), but cannot get it working within a for loop in order to extract the information for any number of locations.

I am hoping someone can show me how to get the for loop working, as I know this should e simple, but I’m just not able to get it.

Cheers

1 Like

As an update, I have also tried this process found in the thread below

And I get the following output on my app. Basically no values pop up in the listviewer.

My blocks are shown below

The key here is that when you use a [for each item] loop, you are not using an integer index (1, 2, 3, etc.). You are actually getting an item from the list which in this case is an object, therefore j is an object. So you can’t use j as your item # for your [in list ___ get #] block.

Here’s how this should be set up:

Just remove the [in list ___ get #] block and use [ j ] instead.

If that doesn’t work for very long lists of locations, you may need to add a wait block at the end of the loop (try wait 0 seconds, then if that doesn’t work try wait 0.03 seconds).

1 Like

That works perfectly and is an amazing explanation. Thanks so much @tatiang !!!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.