I want to transform the nested JSON structure into a single object, with the dynamic key I tried with below code but it's work only with one level, I need to write some recursive function, that I am struggling to write the code for n level of nested JSON. Please advise.
data.map((e) => {
for (let key in e) {
if (typeof e[key] === "object") {
for (let onLevel in e[key]) {
e[key + "." + onLevel] = e[key][onLevel];
}
}
}
});
Example
Input JSON
[{
"Id": "0hb3L00000000jkQAA",
"Name": "P-2797",
"ContactEncounterId": "0ha3L000000001qQAA",
"StartTime": "2020-06-27T11:00:00.000Z",
"EncounterDuration": 25,
"ContactEncounter": {
"Name": "Grocery Shopping 17",
"LocationId": "1313L0000004ENlQAM",
"Id": "0ha3L000000001qQAA",
"Location": {
"Name": "Waitrose",
"LocationType": "Site",
"Id": "1313L0000004ENlQAM"
}
}
}]
OutPut JSON
[{
"Id": "0hb3L00000000jkQAA",
"Name": "P-2797",
"ContactEncounterId": "0ha3L000000001qQAA",
"StartTime": "2020-06-27T11:00:00.000Z",
"EncounterDuration": 25,
"ContactEncounter.Name": "Grocery Shopping 17",
"ContactEncounter.LocationId": "1313L0000004ENlQAM",
"ContactEncounter.Id": "0ha3L000000001qQAA",
"ContactEncounter.Location.Name": "Waitrose",
"ContactEncounter.Location.LocationType": "Site",
"ContactEncounter.Location.Id": "1313L0000004ENlQAM"
}]