Good morning,
I'm having a problem mounting a JSON using NodeJS.
I have a return from a giant SQL that basically follows the structure below.
[
{
"numDoc":"0000001",
"OutrosCampos":"outrosDados",
"itens":[
{
"itemNum":"000000001",
"nome":"AAAAAAAAA",
"agrup":"00003",
"numDoc":"0000001"
},
{
"itemNum":"000000002",
"nome":"BBBBBBB",
"agrup":"00003",
"numDoc":"0000001"
},
{
"itemNum":"000000003",
"nome":"CCCCCCCCCC",
"agrup":"00003",
"numDoc":"0000001"
}
]
}
]
I need to have my JSON follow the following output structure:
[
{
"numDoc":"0000001",
"OutrosCampos":"outrosDados",
"itens":[
{
"agrup":"00003",
"itensAgrup":[
{
"itemNum":"000000001",
"nome":"AAAAAAAAA"
},
{
"itemNum":"000000002",
"nome":"BBBBBBB"
}
]
},
{
"agrup":"00004",
"itensAgrup":[
{
"itemNum":"000000003",
"nome":"CCCCCCCCCC"
}
]
}
]
}
]
In short, I need the first array (which has the numDoc
) to remain intact, that the second array (the items) be separated by an array by the array, in which there is no longer the repeated field in a DOC and neither the array If it was possible.
I am using nodeJS and also several "packages" as the underscore but I am not able to transform the array in this JSON.
NOTE: I need to read key and value, since this structure can vary the field names and quantity of items, so I can not try to put it in a fixed array.
Any doubt, you can speak. Thanks for the help right away.