3
Good morning,
I’m having a problem assembling a JSON using Nodejs.
I have a return of 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"
}
]
}
]
and I need my JSON to 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
) remain intact, that the second array (the items) is separated by an array by the grouping, in which the repeated field numDOC no longer appears and nor the grouping if 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, because this structure can vary the field names and the amount of items, so I can’t try to put in a fixed "array".
Any doubt, you can talk.
Thanks for your help from now on.
opa, using your answer, if I wanted to enter another information, before grouping items, type, {... {agrup : 000003, groupname : ZZZZZZZ, items: [... add only this line name agrup, which is, in the original array, along with the code of the items, as it would be ?
– Carlos GetC