# Dynamically build Tree items binding from tag path

**URL:** <https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [June 16, 2022, 11:44pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869 "2022-06-16T23:44:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tashby](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tashby/32/67562_2.png) [@tashby](https://forum.inductiveautomation.com/u/tashby)\
**Post date:** [June 16, 2022, 11:44pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/1 "2022-06-16T23:44:23Z")

</div>

I’d like to know how to make a binding that builds the items for a Perspective Tree component automatically. So basically, if I have a tag path that is “Item1/Child1/Grandchild 1”, I can have the items of the Tree Component populate accordingly to make that view. Not sure how to make an “items” binding.

---

<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:** [June 17, 2022, 9:25am UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/2 "2022-06-17T09:25:15Z")

</div>

You need to use a script transform to build a list of your items, following the structure of the items prop for nested items.

[https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Tree](https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Tree)

Note you can create an example structure and copy it, and paste into notepad to get the example json.if you give more specifics of what you’re doing we can help more with some example 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:** [June 17, 2022, 9:59am UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/3 "2022-06-17T09:59:21Z")

</div>

So, a tag browse tree ?  
[https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Tag+Browse+Tree](https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Tag+Browse+Tree)

---

<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:** [June 17, 2022, 1:54pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/4 "2022-06-17T13:54:08Z")

</div>

I was going to suggest that, but sounded like they wanted only specific tags 🤷 I also haven’t used that yet (but will next week actually)

---

<div class="post-metadata">

**Author:** ![tashby](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tashby/32/67562_2.png) [@tashby](https://forum.inductiveautomation.com/u/tashby)\
**Post date:** [June 17, 2022, 3:49pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/5 "2022-06-17T15:49:45Z")

</div>

Ideally, I’d like it to work with the Perspective Tree component as I have a little more control of how I want it to look.

Basically, we have a view that shows additional information about a device. In this case, we show the tag path. I have something working by parsing the tag path and listing each folder that looks like this.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/3/a3923272777cb3b74c9edefb4bda404b69acef36.png)

But, I’d prefer to show the tree view look instead. like this.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/0/e08f317f0485a8ba6da6dd83efbe8634aaf41b22.png)

Where I’m stuck is the syntax of the script to build the json out of the tag folder string.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/2/a2ee762249027bbfaef0f608514646a3d03a7ad8.png)

So how to turn this:  
`Treatment/3KNG/200/100_01_Basin1_Mixer1`

into this:

```auto
[
  {
    "label": "Treatment",
    "expanded": true,
    "items": [
      {
        "label": "3KNG",
        "expanded": true,
        "items": [
          {
            "label": 200,
            "expanded": true,
            "items": [
              {
                "label": "100_01_Basin1_Mixer1",
                "expanded": false,
                "items": []
              }
            ]
          }
        ]
      }
    ]
  }
]

```

---

<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:** [June 17, 2022, 4:40pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/6 "2022-06-17T16:40:33Z")

</div>

Something like this, perhaps?

```python
path = "Treatment/3KNG/200/100_01_Basin1_Mixer1"

parent = []
ret = parent
for part in path.split("/"):
    item = {
        "label": part,
        "expanded": True,
        "items": parent
    }
    parent.append(item)
    parent = []

return ret

```

---

<div class="post-metadata">

**Author:** ![Matthew.gaitan](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Matthew.gaitan](https://forum.inductiveautomation.com/u/Matthew.gaitan)\
**Post date:** [June 17, 2022, 5:03pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/7 "2022-06-17T17:03:22Z")

</div>

I had to make the following changes to get this working:

```auto
path = "Treatment/3KNG/200/100_01_Basin1_Mixer1"
	
parent = []
ret = parent
for part in path.split("/")[::-1]:
    item = {
        "label": part,
        "expanded": True,
        "items": parent
    }
    parent = [item]
    ret = parent
	
return ret

```

---

<div class="post-metadata">

**Author:** ![tashby](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tashby/32/67562_2.png) [@tashby](https://forum.inductiveautomation.com/u/tashby)\
**Post date:** [June 17, 2022, 5:03pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/8 "2022-06-17T17:03:35Z")

</div>

Something about it doesn’t quite seem to be returning an actual array.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/2/02807b2d8d082037438afd8910553575bb501f17.png)

---

<div class="post-metadata">

**Author:** ![tashby](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tashby/32/67562_2.png) [@tashby](https://forum.inductiveautomation.com/u/tashby)\
**Post date:** [June 17, 2022, 5:08pm UTC](https://forum.inductiveautomation.com/t/dynamically-build-tree-items-binding-from-tag-path/60869/9 "2022-06-17T17:08:47Z")

</div>

That worked. Thanks guys!

I did one last change by changing “path” to “value” to bring in the actual property itself.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/b/3b37cca2945530103d06bd76883be03d1272913d.png)
