# Array property can't be iterated through

**URL:** <https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [April 4, 2025, 3:55pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724 "2025-04-04T15:55:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [April 4, 2025, 3:55pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/1 "2025-04-04T15:55:11Z")

</div>

I'm trying to iterate through an array property (this.props.items) in a binding transform. For some reason, whether I use the `.get()` method or brackets to search through the array, I get an error message saying that my key/index has the wrong type.

Specifically: when I try to use a string as the index, it asks for an int, and when I try to use an int as an index, it asks for a string.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/1/0168fb3cea865c22a0e8190fc1716e353f2c334e.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/d/0d696e78142809ff33c25bb538c3e75d6050424a.png)

Why is this happening? I don't recall ever getting this error in the past when iterating through JsonifiableArrayLists.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 4, 2025, 3:56pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/2 "2025-04-04T15:56:10Z")

</div>

Post code - not pictures of code! (Use screengrabs as well if context is useful.) Please see [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [April 4, 2025, 4:03pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/3 "2025-04-04T16:03:38Z")

</div>

Datasource (the `value` parameter in the transform functions below):

```python
[
  {
    "label": "GPA",
    "expanded": true,
    "icon": {
      "path": "material/business",
      "color": "",
      "style": {}
    },
    "data": {
      "EqID": 1,
      "EqType": "enterprise",
      "EqEnabled": true,
      "EqName": "GPA",
      "EqPath": "GPA"
    },
    "items": [
      {
        "label": "Virginia",
        "expanded": true,
        "icon": {
          "path": "material/location_city",
          "color": "",
          "style": {}
        },
        "data": {
          "EqID": 1,
          "EqType": "site",
          "EqEnabled": true,
          "EqName": "Virginia",
          "EqPath": "GPA/Virginia"
        },
        "items": [
          {
            "label": "Mixing",
            "expanded": true,
            "icon": {
              "path": "material/business_center",
              "color": "",
              "style": {}
            },
            "data": {
              "EqID": 1,
              "EqType": "area",
              "EqEnabled": true,
              "EqName": "Mixing",
              "EqPath": "GPA/Virginia/Mixing"
            },
            "items": [
              {
                "label": "Mixer 1",
                "expanded": false,
                "icon": {
                  "path": "material/line_style",
                  "color": "",
                  "style": {}
                },
                "data": {
                  "EqID": 1,
                  "EqType": "line",
                  "EqEnabled": true,
                  "EqName": "Mixer 1",
                  "EqPath": "GPA/Virginia/Mixing/Mixer 1"
                },
                "items": [
                  {
                    "label": "Drum",
                    "expanded": false,
                    "icon": {
                      "path": "material/apartment",
                      "color": "",
                      "style": {}
                    },
                    "data": {
                      "EqID": 1,
                      "EqType": "cell",
                      "EqEnabled": true,
                      "EqName": "Drum",
                      "EqPath": "GPA/Virginia/Mixing/Mixer 1/Drum"
                    },
                    "items": []
                  },
                  {
                    "label": "Sweeper",
                    "expanded": false,
                    "icon": {
                      "path": "material/apartment",
                      "color": "",
                      "style": {}
                    },
                    "data": {
                      "EqID": 3,
                      "EqType": "cell",
                      "EqEnabled": true,
                      "EqName": "Sweeper",
                      "EqPath": "GPA/Virginia/Mixing/Mixer 1/Sweeper"
                    },
                    "items": []
                  }
                ]
              },
              {
                "label": "Mixer 2",
                "expanded": false,
                "icon": {
                  "path": "material/line_style",
                  "color": "",
                  "style": {}
                },
                "data": {
                  "EqID": 5,
                  "EqType": "line",
                  "EqEnabled": true,
                  "EqName": "Mixer 2",
                  "EqPath": "GPA/Virginia/Mixing/Mixer 2"
                },
                "items": [
                  {
                    "label": "Drum",
                    "expanded": false,
                    "icon": {
                      "path": "material/apartment",
                      "color": "",
                      "style": {}
                    },
                    "data": {
                      "EqID": 4,
                      "EqType": "cell",
                      "EqEnabled": true,
                      "EqName": "Drum",
                      "EqPath": "GPA/Virginia/Mixing/Mixer 2/Drum"
                    },
                    "items": []
                  }
                ]
              },
              {
                "label": "Mixer 3",
                "expanded": false,
                "icon": {
                  "path": "material/line_style",
                  "color": "",
                  "style": {}
                },
                "data": {
                  "EqID": 8,
                  "EqType": "line",
                  "EqEnabled": true,
                  "EqName": "Mixer 3",
                  "EqPath": "GPA/Virginia/Mixing/Mixer 3"
                },
                "items": []
              },
              {
                "label": "Mixer 4",
                "expanded": false,
                "icon": {
                  "path": "material/line_style",
                  "color": "",
                  "style": {}
                },
                "data": {
                  "EqID": 9,
                  "EqType": "line",
                  "EqEnabled": true,
                  "EqName": "Mixer 4",
                  "EqPath": "GPA/Virginia/Mixing/Mixer 4"
                },
                "items": [
                  {
                    "label": "Cell 1",
                    "expanded": false,
                    "icon": {
                      "path": "material/apartment",
                      "color": "",
                      "style": {}
                    },
                    "data": {
                      "EqID": 10,
                      "EqType": "cell",
                      "EqEnabled": true,
                      "EqName": "Cell 1",
                      "EqPath": "GPA/Virginia/Mixing/Mixer 4/Cell 1"
                    },
                    "items": []
                  },
                  {
                    "label": "Cell 2",
                    "expanded": false,
                    "icon": {
                      "path": "material/apartment",
                      "color": "",
                      "style": {}
                    },
                    "data": {
                      "EqID": 11,
                      "EqType": "cell",
                      "EqEnabled": true,
                      "EqName": "Cell 2",
                      "EqPath": "GPA/Virginia/Mixing/Mixer 4/Cell 2"
                    },
                    "items": []
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]

```

When I try to pass `index` as a string:

```python
def transform(self, value, quality, timestamp):
	final_node = {
		"EqEnabled": "",
		"EqID": "",
		"EqName": "",
		"EqPath": "",
		"EqType": ""
	}
	
	if self.props.selection[0] and len(self.props.selection) > 0: 
		path = self.props.selection[0].split("/") # 0/0/0/0
		current_node = value
		current_node_index = 0
		path_array_final_index = len(path) - 1
		for index in path:
			index = index
			current_node = current_node.get(index)
			current_node_index += 1
			if current_node_index != path_array_final_index:
				current_node = current_node.get("items")
		
		current_node_data = current_node["data"]
		final_node["EqEnabled"] = current_node_data["EqEnabled"]
		final_node["EqID"] = current_node_data["EqID"]
		final_node["EqName"] = current_node_data["EqName"]
		final_node["EqPath"] = current_node_data["EqPath"]
		final_node["EqType"] = current_node_data["EqType"]
	
	return final_node

```

```python
Traceback (most recent call last): File "<transform>", line 16, in transform TypeError: com.inductiveautomation.perspective.gateway.script.JsonifiableArrayList indices must be integers

```

When I try to pass `index` as an integer:

```python
def transform(self, value, quality, timestamp):
	final_node = {
		"EqEnabled": "",
		"EqID": "",
		"EqName": "",
		"EqPath": "",
		"EqType": ""
	}
	
	if self.props.selection[0] and len(self.props.selection) > 0: 
		path = self.props.selection[0].split("/") # 0/0/0/0
		current_node = value
		current_node_index = 0
		path_array_final_index = len(path) - 1
		for index in path:
			index = index
			current_node = current_node.get(int(index))
			current_node_index += 1
			if current_node_index != path_array_final_index:
				current_node = current_node.get("items")
		
		current_node_data = current_node["data"]
		final_node["EqEnabled"] = current_node_data["EqEnabled"]
		final_node["EqID"] = current_node_data["EqID"]
		final_node["EqName"] = current_node_data["EqName"]
		final_node["EqPath"] = current_node_data["EqPath"]
		final_node["EqType"] = current_node_data["EqType"]
	
	return final_node

```

```python
Traceback (most recent call last): File "<transform>", line 17, in transform TypeError: expected a str

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 4, 2025, 4:17pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/4 "2025-04-04T16:17:21Z")

</div>

Sometimes `current_node` is an object, and sometimes it's an array. So whichever indexing strategy you attempt isn't valid, some of the time.

---

<div class="post-metadata">

**Author:** ![Noah\_Gilgo](https://avatars.discourse-cdn.com/v4/letter/n/c89c15/32.png) [@Noah\_Gilgo](https://forum.inductiveautomation.com/u/Noah_Gilgo)\
**Post date:** [April 4, 2025, 4:36pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/5 "2025-04-04T16:36:48Z")

</div>

But `current_node` in `current_node = current_node.get(int(index))` is always going to be the array type in every instance that the line of code is called. So ideally, there should be a method of indexing arrays that always works for this line of code.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 4, 2025, 5:23pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/6 "2025-04-04T17:23:27Z")

</div>

```python
item = {'items': value}
for n in self.props.selection.data.split('/'):
    item = item['items'][int(n)]

```

But... What exactly are you trying to do ? The whole thing looks quite weird to me...

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [April 4, 2025, 5:27pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/7 "2025-04-04T17:27:19Z")

</div>

Looks like they are trying to traverse a tree structure. Some of my scripts to get the selected item out of a tree component looked similar when I first started. `selectedData` has some weird behavior when you are updating the tree structure.

---

<div class="post-metadata">

**Author:** ![vtran](https://avatars.discourse-cdn.com/v4/letter/v/779978/32.png) [@vtran](https://forum.inductiveautomation.com/u/vtran)\
**Post date:** [April 4, 2025, 6:29pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/8 "2025-04-04T18:29:56Z")

</div>

For your purpose, looks like using `self.props.selection.data` would be enough like @pascal.fragnoud suggested. Though, if you still want to traverse the `items` tree and also grab all its children's data, you can use something like this

```python
indices = [int(x) for x in self.props.selection[0].split("/")]
items = self.props.items
for i in indices:
	item = items[i]
	items = item.items

# at this point, you can extract whatever you want from item

```

credit to @pturmel, this is something I learned from his TagReportUtility exchange resource

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 6, 2025, 1:23pm UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/9 "2025-04-06T13:23:26Z")

</div>

> [@Noah\_Gilgo](#):
>
> But `current_node` in `current_node = current_node.get(int(index))` is always going to be the array type in every instance that the line of code is called

 ![Screenshot_20250406_225032_Chrome](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/a/aad9a14fe23fb1e0636ef2f5691fb3a6caa3f294.jpeg)

No, it starts off as a list, then inside the for it become a dict, then the second for iteration will fail because the current node is now a dict and you can't supply in int index

You should definitely avoid reusing the same variable and morphing it into whatever you want, as it's almost impossible to not be caught by it, case in point

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [April 7, 2025, 6:20am UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/10 "2025-04-07T06:20:50Z")

</div>

I feel like the naming is to blame here.  
The variable is named `current_node`, but it doesn't actually start as the current node. It starts as a list of available nodes for this level.  
This is most likely the source of the confusion.

I can't remember who said that naming things is the hardest thing in programming, but he was partly right. Good names are precious, they make everything else easier. They force you to think hard about what you're doing, and that in itself is a pain and a blessing.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [April 7, 2025, 9:54am UTC](https://forum.inductiveautomation.com/t/array-property-cant-be-iterated-through/104724/11 "2025-04-07T09:54:29Z")

</div>

> [@pascal.fragnoud](#):
>
> naming things is the hardest thing in programming

That and locating things in a logical spot... I always find perspective styles incredibly hard to name and locate
