How to loop through an Array in JSON

I have this sample JSON response
{
“elements1”:[ {
“id”:“1111”,
“lineitems”: {
“elements2”: [ {
“name”: “One”,
“modifications”: {
“elements3”: [ {
“name”: “large”,
“quantity”: 5
}, {
“name”: “medium”,
“quantity”: 10
}
]
}
}, {
“name”: “Two”,
“modifications”: {
“elements3”: [ {
“name”: “large”,
“quantity”: 6
}, {
“name”: “medium”,
“quantity”: 9
}
]
}
}
]
},
“id”:“1111”,
“lineitems”: {
“elements2”: [ {
“name”: “One”,
“modifications”: {
“elements3”: [ {
“name”: “large”,
“quantity”: 5
}
]
}
}, {
“name”: “Three”,
“modifications”: {
“elements3”: [ {
“name”: “large”,
“quantity”: 6
}, {
“name”: “medium”,
“quantity”: 9
}
]
}
}, {
“name”: “Four”,
“modifications”: {
“elements3”: [ {
“name”: “large”,
“quantity”: 6
}, {
“name”: “medium”,
“quantity”: 9
}
]
}
}

]
}
}
]
}

As you can see, elements2 may have none, one or more values.

My question is how can you get the values of elements2 using a dynamic loop?

Thank you in advance for your help.

PS. Sorry, I don’t know how to indent appropriately.

It’s very helpful that you posted the JSON response but it’s not valid JSON. There must be a bracket missing. You can paste your JSON response into Best JSON Viewer and JSON Beautifier Online on the left side and then click Tree Viewer to see the valid JSON (or error message) on the right side.

Edit: It was valid JSON but the forums converter your quotes to non-standard quotes. I replaced them and it now works on CodeBeautify.

This site will indent JSON (click Validate JSON). To keep the indentations when posting on the forums, include three back ticks (```) before and after the text.

{
	“
	elements1”: [{
		“
		id”: “1111”,
		“lineitems”: {
			“
			elements2”: [{
				“
				name”: “One”,
				“modifications”: {
					“
					elements3”: [{
						“
						name”: “large”,
						“quantity”: 5
					}, {
						“
						name”: “medium”,
						“quantity”: 10
					}]
				}
			}, {
				“
				name”: “Two”,
				“modifications”: {
					“
					elements3”: [{
						“
						name”: “large”,
						“quantity”: 6
					}, {
						“
						name”: “medium”,
						“quantity”: 9
					}]
				}
			}]
		},
		“id”: “1111”,
		“lineitems”: {
			“
			elements2”: [{
					“
					name”: “One”,
					“modifications”: {
						“
						elements3”: [{
							“
							name”: “large”,
							“quantity”: 5
						}]
					}
				}, {
					“
					name”: “Three”,
					“modifications”: {
						“
						elements3”: [{
							“
							name”: “large”,
							“quantity”: 6
						}, {
							“
							name”: “medium”,
							“quantity”: 9
						}]
					}
				}, {
					“
					name”: “Four”,
					“modifications”: {
						“
						elements3”: [{
							“
							name”: “large”,
							“quantity”: 6
						}, {
							“
							name”: “medium”,
							“quantity”: 9
						}]
					}
				}

			]
		}
	}]
}

Here’s what the Tree View looks like after replacing the quotes:

See the screen titled “@arty3mm” in this project for an example of how to loop through the “elements2” property:

https://x.thunkable.com/copy/9a4fbb5093b10e308084b402c8a6101e

Note that I’m using a shorthand property reference of “elements1[1].lineitems.elements2”. This is the same as getting the “elements2” property from the “lineitems” property from the first item of the list generated by the “elements1” property.

1 Like

Hi Tatiang,

Thank you for your answer. I was able to see your output and how your blocks work.

Can I update the question to how are you going to display the JSON data like the one below:

ID Name Size Quantity
1111 One Large 5
1111 One Medium 10
1111 Two Large 6
1111 Two Medium 9

1111 One Large 5
1111 Three Large 6
1111 Three Medium 9
1111 Four Large 6
1111 Four Medium 9

Each line can can be displayed in a single label joined together with wait control like in your sample blocks.

Thanks.

Here’s an updated project that includes references to each of the properties you’re wanting:

https://x.thunkable.com/copy/80a8d28669a36e41bf91fc26f8474c74

It doesn’t loop through them but it may get you started on figuring out how to do that.

1 Like

Hi Tatiang,

Your last suggestion was the one I started before but I discovered that elements2 is a dynamic list, it can be none, one or more. This prompted me to ask for help.

Can I use For-Each Block within For-each Block, the outer For-Each will be for the Name in elements1 and the inner For-Each will be for the Name and Modifications in elements2? But I’m not sure what to put in the Get-Property and Of-Object in the inner For-Each block.

Thanks.

Yes, you can nest loop blocks. You may have to adjust the timing a little with a Wait 0 Seconds block (which actually waits more than 0 seconds!) but I’d try it first without any delays and see if it works. If you get stuck, post screenshots or a link to your project and we can go from there.

Another option is to loop through the outer JSON array/list and store that in a variable. Then, in the second loop, use that variable as input for parsing the next info. That way you don’t have to worry as much about the timing.