Hello, I have below json coming from API
JSON
{
"id": 1,
"behaviour": "Attention",
"operationalDef": "What did I just say? This is definition of Attention",
"acD1": "aa1",
"acD2": "aa2",
"acD3": null,
"acD4": null,
"customACD1": null
}
]I would like to convert the same into below form. Basically I want to make a parent object that will contain the values of acD1 to acD4.
JSON
{
"id": 1,
"behaviour": "Attention",
"operationalDef": "What did I just say? This is definition of Attention",
"options" : ["aa1","aa2"],
"customACD1": null
}
]
Vijay Pratap SinghRank #381 · 6.6K reputationPosted Dec 28, 2023, 10:34 PM EST
You can transform the given JSON structure into the desired form using JavaScript. Here's a simple example using JavaScript
// Original JSON from API const originalJson = { "id": 1, "behaviour": "Attention", "operationalDef": "What did I just say? This is the definition of Attention", "acD1": "aa1", "acD2": "aa2", "acD3": null, "acD4": null, "customACD1": null }; // Transforming the JSON const transformedJson = { id: originalJson.id, behaviour: originalJson.behaviour, operationalDef: originalJson.operationalDef, options: [ originalJson.acD1, originalJson.acD2, originalJson.acD3, originalJson.acD4 ].filter(option => option !== null), customACD1: originalJson.customACD1 }; // Displaying the transformed JSON console.log(transformedJson);Hope this will help you