Apex Controller returns JSON serialized data for LWC and Aura : How to parse it
1 min readMar 5, 2020
Flatten the returned data from your apex code
Data returned from Apex:
return [Select Name, profile.name From user where name like =: key];
Flatten the data so that you can easily access it.
//JS code in LWC or Aurafunction flattenObject(ob) {
let newObject = {}
for (let i in ob) {
if (typeof ob[i] === 'object' && ob[i] !== null) {
retObj = flattenObject(ob[i]);
for(let j in retObj) {
newObject[i + '.' + j] = retObj[j];
}
} else {
newObject[i] = ob[i];
}
}
return newObject;
}