API results into a List Viewer


#1

Hi All,

I have read a bunch of docs and other community articles today but I am finding a task (that I think should be simple) very frustrating! I am trying to get the results of an API into a listviewer to form a list of clickable links that will go to another page and use the relevant ID to load data from another API.

JSON response is as follows
[{“key”:“1”,“value”:“Jindabyne”},{“key”:“2”,“value”:“Cooma”},{“key”:“3”,“value”:“Berridale”},{“key”:“4”,“value”:“Bombala”},{“key”:“5”,“value”:“Adaminaby”},{“key”:“6”,“value”:“Anglers Reach”},{“key”:“7”,“value”:“Dalgety”},{“key”:“8”,“value”:“Michelago”},{“key”:“9”,“value”:“Delegate”},{“key”:“10”,“value”:“Bredbo”},{“key”:“11”,“value”:“Numeralla”},{“key”:“12”,“value”:“Nimmitabel”},{“key”:“13”,“value”:“Jerangle”},{“key”:“14”,“value”:“Craigie”},{“key”:“15”,“value”:“Charlottes Pass”},{“key”:“16”,“value”:“Thredbo”},{“key”:“17”,“value”:“Perisher”},{“key”:“18”,“value”:“Guthega”},{“key”:“19”,“value”:“Mt Selwyn”}]

This is what I have in the blocks.

So I think what is happening is:
When Screen 1 starts
Load data from the API URL
Set my variable ListContents to empty list
Then from the JSON object, insert the ‘name’ key into the list
Set the contents of the List Viewer to the variable ListContents

However all I get is a blank row in the list viewer. Where am I going wrong?

Secondly when the user clicks one of these items (once populated) how do I use the key (integer) with the button and pass it to the next screen?

Thanks!


#2

It looks like your JSON response is a representation of a list (you can tell because it starts with a ‘[’ character). Consequently, your

53%20PM

block is returning a list. You’ll need to iterate (i.e. loop) over that list using:

36%20PM

and then use your

20%20PM

block on each of the items in the list, which you can access by using the

20%20PM

block that you will be able to find in the “Variables” category of the Blocks editor.

In that loop, you’ll want to just insert the ‘value’ property of each item ‘j’ into your ‘ListContents’.

I hope this helps.

-Mark


#3

Hey @Mark,
I’m taking a stab at an API project using your approach above without success.

From my understanding, this is how I would loop through a list of JSON objects returned through an API, but this is not returning any results:

Is this the correct approach or am I off base?

Thanks,
Darren


Appendix 1
Here is my data:

[{
“id”: 2928,
“name”: “Abita Brewing Co”,
“brewery_type”: “regional”,
“street”: “166 Barbee Rd”,
“city”: “Covington”,
“state”: “Louisiana”,
“postal_code”: “70433-8651”,
“country”: “United States”,
“longitude”: null,
“latitude”: null,
“phone”: “9858933143”,
“website_url”: “http://www.abita.com”,
“updated_at”: “2018-08-11T21:37:28.656Z”,
“tag_list”: []
}, {
“id”: 5130,
“name”: “Habitat Brewing Co”,
“brewery_type”: “micro”,
“street”: “174 Broadway St”,
“city”: “Asheville”,
“state”: “North Carolina”,
“postal_code”: “28801-2305”,
“country”: “United States”,
“longitude”: “-82.5533973”,
“latitude”: “35.5986648”,
“phone”: “7047018141”,
“website_url”: “http://www.habitatbrewing.com”,
“updated_at”: “2018-08-24T15:41:50.670Z”,
“tag_list”: []
}]


Appendix 2
Through my struggles, I was able to brute force the result I was looking for:


#4

Darren,

If you look carefully at my post above, you’ll note that I am using the 'get object from JSON' block directly on the 'response' block. You need to do that because the 'response' is not itself a list. It is JSON text which represents a list. I know it’s a little confusing because in this case the 'get object from JSON' block isn’t returning an object - it’s returning a list.

Once you have the list you can iterate over it an each item j will be one of the objects that you are interested in and that you can get the name of. I.e., you will simply do a 'get property name of object j'.

Does that make sense?

-Mark


#5

@Mark,

Thanks for your response. I got it working now!

The issue is the 'get object from JSON' block will not connect to the 'for each item j in list' block as depicted here:
Not%20Connecting

However if I save the 'get object from JSON - response' block in a list variable 1st, I am then able to connect the variable to the 'for each item j in list' block. Glad to have this working, and I’ve added my final solution below.

Thanks,
Darren


The following shows how to loop through a JSON List that is returned through an API response.
Multiple%20Objects%20Working


#6

Darren,

I’m glad its working now! And I’ve reported the bug that you found whereby you couldn’t connect the 'get object from JSON' block to the 'for each item j in list' block.

-Mark