# Looping through JSON

**URL:** <https://forum.inductiveautomation.com/t/looping-through-json/53900>\
**Category:** Ignition\
**Created:** [December 8, 2021, 7:19pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900 "2021-12-08T19:19:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [December 8, 2021, 7:19pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/1 "2021-12-08T19:19:27Z")

</div>

Hello all,

I have a script transform on a dataset that is bound to the instance of a flex repeater:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/8/7816144a482afab96edecee8509f45e269a9c8bb.png)

In the first part of the code, I am returning 6 instances with their values from the dataset, now I would like to be able to return an index value for each instance just like the picture below:

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/5/858b70446c8d47ab8537333298c91a2480466afa.png)

I am new to JSON and I do not know how to combine the last part with the first part of the code. I would appreciate your help in this regard. Thank you!

---

<div class="post-metadata">

**Author:** ![travis.broussard](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@travis.broussard](https://forum.inductiveautomation.com/u/travis.broussard)\
**Post date:** [December 8, 2021, 7:23pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/2 "2021-12-08T19:23:33Z")

</div>

Well, the first thing to note is that script will end after the ‘return’ statement in the top part of the code. That’s why nothing in the bottom is being executed. You need to modify the list that you’re trying to return in script, and once it’s completely modified, then return it.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [December 8, 2021, 7:24pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/3 "2021-12-08T19:24:48Z")

</div>

You are correct! That was just for the sake of demonstration so that I know which part returns what as I tried them separately.

I wrote this script too, which I know is wrong but cannot figure out how to do it:  
I get “mismatched input ‘for’ expecting RCURLY” error

```auto
	return [
		{
			'mes_spec':spec,
			'spec_rev':spec_rev,
			'name':name,
			'description':description,
			'state':state,
			'findings':findings,
			'low_limit': low,
			'process_value': proc,
			'high_limit': high,
			'units':units,
			for row in range(value.getRowCount()):
				dict = {
					'index': row
					}
			list.append(dict)
		} for spec,spec_rev,name,description,state,findings,low, proc, high, units in system.dataset.toPyDataSet(value)
	]

```

---

<div class="post-metadata">

**Author:** ![travis.broussard](https://avatars.discourse-cdn.com/v4/letter/t/db5fbb/32.png) [@travis.broussard](https://forum.inductiveautomation.com/u/travis.broussard)\
**Post date:** [December 8, 2021, 7:36pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/4 "2021-12-08T19:36:41Z")

</div>

I might be misunderstanding what you’re trying to achieve, but if looks like you’re trying to return the dataset as JSON and add an “index” field that increments to each row. An easy way to achieve this would be to do something akin to this:

```auto
x = 0
list = []

pyData=system.dataset.toPyDataSet(value)
for row in pyData:

    dict = {"mes_spec": row["spec"], "spec_rev": row["spec_rev"], .... , "index": x}
    list.append(dict)
    x += 1

return list

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 8, 2021, 7:43pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/5 "2021-12-08T19:43:43Z")

</div>

1. Don’t use ‘List’ as a variable name. This is a terrible practice even if you know it’s wrong and you’re only writing test code. Just kick the habit of using python built in names as variables, so nothing like

```python
list,dict,str,int,tuple,set,float

```

should ever be used as variable names. And there are many others besides those

1. You can enumerate the instances and add the index to them in the for loop with the `enumerate()` function.

Something like this should do:

```python
	instances = [
		{
			'mes_spec':spec,
			'spec_rev':spec_rev,
			'name':name,
			'description':description,
			'state':state,
			'findings':findings,
			'low_limit': low,
			'process_value': proc,
			'high_limit': high,
			'units':units
		} for spec,spec_rev,name,description,state,findings,low, proc, high, units in system.dataset.toPyDataSet(value)
	]

    for r,i in enumerate(instances):
    	i['index'] = r

    return instances

```

1. It should be noted that what you are doing here isn’t truly JSON, to the Jython this just looks like a list of dictionaries and you can treat it that way.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 8, 2021, 7:50pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/6 "2021-12-08T19:50:04Z")

</div>

What @lrose said. But also, you’re trying to embed procedural code (a `for` loop) inside an expression (returning a dictionary inside a list). Once you begin an expression, everything inside it must also be an expression. The `.append()` method is not an expression. Assignments of any kind (`=`, `+=`, etc.) are also not valid in expressions.

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [December 8, 2021, 7:50pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/7 "2021-12-08T19:50:49Z")

</div>

Thank you very much @lrose . Great information always. Implemented your script but I get an error configuration (“parse error”)

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [December 8, 2021, 7:51pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/8 "2021-12-08T19:51:25Z")

</div>

make sure that all of the indentation characters match, all spaces or all tabs

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [December 8, 2021, 7:57pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/9 "2021-12-08T19:57:31Z")

</div>

Thank you so much @lrose. That worked! And it was a clever solution as always!  
I do really appreciate it!

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [December 8, 2021, 7:59pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/10 "2021-12-08T19:59:24Z")

</div>

Thank you very much for the information, Phill. Appreciate it! BTW, happy anniversary of the day you joined this community 🎉

---

<div class="post-metadata">

**Author:** ![pkhoshroo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pkhoshroo/32/26801_2.png) [@pkhoshroo](https://forum.inductiveautomation.com/u/pkhoshroo)\
**Post date:** [December 8, 2021, 8:00pm UTC](https://forum.inductiveautomation.com/t/looping-through-json/53900/11 "2021-12-08T20:00:26Z")

</div>

Thank you very much, Travis! I tried this, but I get an error and it says column spec does not found which is odd
