My JSON input is as follows:
[
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
...
]
I am looking to transform the JSON data based on parent categories and format the child elements under each parent in a specific way. The transformation should be dynamic without hardcoding any comparisons with strings.
The desired output format of the JSON should resemble the following:
[
{
"Choclate": [
{
null: [
{
"2022-05-01": {
"value": 21052631.657999996
}
}
]
},
{
"ABC": [
{
"2022-05-01": {
"value": 505567
}
}
]
},
...
]
},
{
"Drink":[
{
"ABC": [
{
"2022-05-01": {
"value": 882010439.286
}
}
]
},
...
]
}
]
I have attempted to separate the parents and child values into arrays, but I've been unsuccessful so far. Please provide suggestions on how to achieve the transformation from the input JSON to the desired output JSON format.
const data =[
{
"date": {
"value": "2022-05-01"
},
"parent": {
"value": "Choclate"
},
"child": {
"value": null,
"filterable_value": "EMPTY"
},
"qty": {
"value": 21052631.657999996,
"rendered": "21.05M"
}
},
...
];
let uniqueparent = [...new Set(data.map(item => item['parent']['value']))];
let uniquechild = [...new Set(data.map(item => item['child']['value']))];
console.log(uniqueparent);
console.log(uniquechild);
From this point, I'm uncertain how to proceed with transforming my data into the desired JSON format.