How to create a JSON tree based on variable

Hello,
I use http request to get structure of my configuration and I am getting array of objects like this:
image

There are 11 capabilities of variable called TYPE (1-11). Program should go through every object in this array and groups types together . For example array of types == 9 should have 5 objects which every object has his own “TYPE”,“NAME”,“UNIT”

The reason of that is because I want to create a configuration tree in other request, based on variable called type.
This tree is a JSON body and looks like this:

And for example array of objects which type == 9 should be placed in children of HWD.
Program should copy every random tree, not only this one.
Maybe there is a easier way to copy this tree I will willingly know your opinion.
Thanks for any help.

Okay, I wrote some code:


It groups every object which has type == 9. It works like I want to but i have a problem with passing this HWD_9 array into my json body.
I am trying like this:
image
but unfortunately it doesnt work.

This is console.log(pm.globals.get(‘HWD_9’)):
image
Name, type and others should be in the " ". How can I do this?