0

I'm not that pro at JSON I have some knowledge but I think not that deep knowledge and I want to create a custom JSON from an array of objects. I hope you can help me.

I'm using JavaScript and NodeJS....

So I have the next data coming from my backend (array of objects):

enter image description here

and I want to create a JSON like this:

{
"items":[
    {
        "name":"extintor de agua",
        "itemData":[
            {
                "caracteristica":"item 1", "header": "Header 1", "id":12867
            },
            {
                "caracteristica":"item 2", "header": "Header 1", "id":12870
            }
        ]
    },
    {
        "name":"Extintor CO2",
        "itemData":[
            {
                "caracteristica":"item 1", "header": "Header 1", "id":12873
            },...
        ]
    }
]

}

As you can see I retrieve multiple rows for the same Entity field for example: multiple rows for "extintor the agua" and multiple rows for "extintor CO2" and so on with many more items. In the JSON you can see that I'm assigning the Entity field one time as "name" and inside that a "item Data" that contains all the values from the multiple "extintor de agua" values.

can I do that with JSON? if so how can I do it. If the problem needs more explanation I can give it I hope you can help me :)

  • 1
    JSON isn't really part of the equation here. What you're looking to do is take one data structure and convert it into another, which is entirely possible using the existing methods of arrays and objects. – Kevin B Jan 17 '22 at 20:20
  • It seems to me the JSON needs to be restructured. Instead of having several items for the same element, it would be wise to group them all together. – Grogu Jan 17 '22 at 20:22
  • you have any insight on it pls, I'm really blocked right now :( – Sergio Morera Sanchez Jan 17 '22 at 20:22
  • I just created a solution for you, Sergio! check it out on codepen https://codepen.io/oaraujocesar/pen/qBPvwxJ if you have any doubts, just comment here. – César O. Araújo Jan 17 '22 at 20:43
  • Hey i just loved your solution but it is not letting me iterate over it and accessing to the values :( – Sergio Morera Sanchez Jan 17 '22 at 21:47

0 Answers0