# How to loop through an Array in JSON

**URL:** <https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862>\
**Category:** Questions about Thunkable X\
**Created:** [November 13, 2021, 10:42pm UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862 "2021-11-13T22:42:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arty3mm](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@arty3mm](https://community.thunkable.com/u/arty3mm)\
**Post date:** [November 13, 2021, 10:42pm UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/1 "2021-11-13T22:42:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 13, 2021, 11:08pm UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/2 "2021-11-13T23:08:36Z")

</div>

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](https://codebeautify.org/jsonviewer) 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](https://jsonlint.com/) will indent JSON (click Validate JSON). To keep the indentations when posting on the forums, include three back ticks (```) before and after the text.

```auto
{
	“
	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:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/8/b/8bf20e2a7b8c7c133985549aa622a1642b74a383.png)

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 13, 2021, 11:32pm UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/3 "2021-11-13T23:32:05Z")

</div>

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](https://x.thunkable.com/copy/9a4fbb5093b10e308084b402c8a6101e)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/6/a/6a8d94f8b2abb6637f8ceadf5d259fda6b309c4f.png)

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.

---

<div class="post-metadata">

**Author:** ![arty3mm](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@arty3mm](https://community.thunkable.com/u/arty3mm)\
**Post date:** [November 14, 2021, 3:58am UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/4 "2021-11-14T03:58:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 14, 2021, 5:37am UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/5 "2021-11-14T05:37:52Z")

</div>

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

[https://x.thunkable.com/copy/80a8d28669a36e41bf91fc26f8474c74](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.

---

<div class="post-metadata">

**Author:** ![arty3mm](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@arty3mm](https://community.thunkable.com/u/arty3mm)\
**Post date:** [November 14, 2021, 3:56pm UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/6 "2021-11-14T15:56:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tatiang](https://sea1.discourse-cdn.com/flex015/user_avatar/community.thunkable.com/tatiang/32/55482_2.png) [@tatiang](https://community.thunkable.com/u/tatiang)\
**Post date:** [November 14, 2021, 4:54pm UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/7 "2021-11-14T16:54:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/thunkable/original/3X/0/f/0f59f292712368bce16ff80133ae10de8a6f27e8.png) [@system](https://community.thunkable.com/u/system)\
**Post date:** [February 12, 2022, 4:54pm UTC](https://community.thunkable.com/t/how-to-loop-through-an-array-in-json/1613862/8 "2022-02-12T16:54:53Z")

</div>

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