# Programmatically create json hierarchy

**URL:** <https://forum.inductiveautomation.com/t/programmatically-create-json-hierarchy/82920>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [December 8, 2023, 10:48pm UTC](https://forum.inductiveautomation.com/t/programmatically-create-json-hierarchy/82920 "2023-12-08T22:48:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [December 8, 2023, 10:48pm UTC](https://forum.inductiveautomation.com/t/programmatically-create-json-hierarchy/82920/1 "2023-12-08T22:48:31Z")

</div>

To pythonians.  
This is the hardest for me - cause it involve recursive

Consider the navigation tree (perspective tree component):  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/9/390fdb10fa50d5c37bd10670b4c81b3d7e496b7a.png)

How do I build this programmatically, given a table from database:  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/f/3fcdbd0f69392c3a963905c6f426934a39b1aab5.png)

(is the table structured even right?)  
In other words, how do I programmatically walk thru the table above to create the code below.

```auto
[
  {
    "label": "Main",
    "items": [
      {
        "label": "Prod A",
        "items": [
          {
            "label": "Line 1",
            "items": []
          },
          {
            "label": "Line 2",
            "items": []
          }
        ]
      },
      {
        "label": "Prod B",
        "items": [
          {
            "label": "Line 3",
            "expanded": false,
            "items": []
          },
          {
            "label": "Line 4",
            "items": []
          }
        ]
      }
    ]
  }
]

```

I started with the below function:

```auto
	def getItems(<some query here>):
		itemObj={}
		itemObj['label'] = 'USA'
		itemObj['items'] = []
		return itemObj

```

I can't think how to start my `for loop`

---

<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:** [December 8, 2023, 11:03pm UTC](https://forum.inductiveautomation.com/t/programmatically-create-json-hierarchy/82920/2 "2023-12-08T23:03:59Z")

</div>

@Matthew.gaitan helped me solve a similar problem. In my application I used a Tree component as a navigation tree. The tree was populated by a text file very similar to the database data you've shown there. It worked out really well and the text file made editing the site menu a very simple task using a text field within the application (with appropriate user rights). I documented my final code so I hope it will be of use to you.

> [@Text tree to Tree component 'items' property](https://forum.inductiveautomation.com/t/text-tree-to-tree-component-items-property/75453):
>
> I'm experimenting with using the Tree component for navigation rather than the Menu Tree. It offers the user clear visibility of where in the site they are whereas the Menu Tree doesn't. I'm making the tree configurable within the application (subject to user roles) and want to use a simple markup structure to generate the Tree's props.items. Markup would look like this with the syntax anchor text | page URL. The URL can be passed to the tree's onActionPerformed event to cause a navigation to …

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [December 8, 2023, 11:58pm UTC](https://forum.inductiveautomation.com/t/programmatically-create-json-hierarchy/82920/3 "2023-12-08T23:58:45Z")

</div>

Hi Transistor, thank you for this, no doubt this is the solution,  
but I do not follow on:

- Create a source of the menu structure in text.
- Create an Expression Binding on the Tree's `props.items` to your text.

It is blur for me,  
How do you put the "structured text" on the expression field on the binding?

---

<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:** [December 9, 2023, 12:04am UTC](https://forum.inductiveautomation.com/t/programmatically-create-json-hierarchy/82920/4 "2023-12-09T00:04:30Z")

</div>

Sorry, bedtime in Ireland. I'll look at it tomorrow.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [December 9, 2023, 5:08am UTC](https://forum.inductiveautomation.com/t/programmatically-create-json-hierarchy/82920/5 "2023-12-09T05:08:09Z")

</div>

I got it.

I literally, copied the code below.  
The input is a column from dataset with `completePath'.  
Work like a charm.  
Thank you for replying to my post quick.

> [@Text tree to Tree component 'items' property](https://forum.inductiveautomation.com/t/text-tree-to-tree-component-items-property/75453/2):
>
> I've been using a modified version of this: I'm using this code in a script transform. Original binding is to a dataset containing path data. ex: "A/B/C" items = [] for row in range(value.rowCount): path = value.getValueAt(row, 0) current = items for part in path.split("/"): # Check if the current folder exists in our items list folderExists = False for itemsPointerItem in current: if part == itemsPointerItem['label']: folderExists = True current = itemsPointerItem['ite…
