Working with a JSON API Array

Hi

I have tried to look at past threads on this but I just can’t read my head round how I am supposed to handle an array of objects.

This is what I have so far:

I can get eg. record 1 by setting but I don’t understand how to get all of them, I know once I do this to then put them in a list.

Tried searching for a tutorial on this but couldn’t find one.

Thanks!

You should be able to loop from 1 to the length of the JSON array (it looks like there are 10 entries but I’m not sure how to get this dynamically) and use the loop index variable (e.g. i) as the “1” in your get property field. As you get the value, add it as the last item in a list instead of setting app apiResponse to the value.

I went to https://wowfreebies.co.uk/wp-json/wp/v2/offer and copied the JSON into a text string assigned to a variable. But I’m having a hard time parsing it because I don’t know the categories values. What’s an example of a ?categories= value that works with this API?

Edit: oh, 9 works.

Hmm. I’m used to JSON objects but this is presenting an array (starting with a bracket [ ). I had thought a list block might work with it but I haven’t had any luck.

2 Likes

I love me some JSON!

Here is my approach:
Set APP apiResponse to Get Object From JSON -> Response
For each i in APP apiResponse
FROM idTest SET text TO Generate JSON from Object -> i

That should iterate through the list displaying the JSON for each item. You can progressively parse the results. You might also check out JSON Explorer/Browser see what the results look like.

Feel free to DM me with specific questions. I use JSON almost exclusively for data storage, so I’ve got tons of tips and tricks.

3 Likes

Here’s an example that works with a set # of array items (5):

1 Like

I was thinking the same thing. And that does work. I’ll post a dynamic version of the blocks in a minute.

1 Like

tatiang,
find that the For Each is super handy for JSON arrays vs. For i = X to Y, they save the step of figuring out how long the array is.

1 Like

@drted I love toying with JSON too, but I find it super frustrating. A good kind of frustrating – usually – but still frustrating.

Alright, this works:

Edit: just realizing that the set app JSON object block should be inside the else section of the if/do block. I was rushing a bit when I made that.

2 Likes