How can I generate dynamic JSON based on input, filter data, and create appropriate key-value pairs for JSON objects?
The database schema can be viewed
https://i.sstatic.net/iP1JS.png
Although I attempted the following code, it did not produce the desired result...
success: function (data) {
//Calculate length
data.value.length;
empData = '[';
$.each(data.value, function (index, item) {
var dataLen = data.value.length;
empData += `{`;
if (item.STATUS == 'Active') {
if (item.NODE == 'Testing') {
empData += `"DDM_CO2" : {
"DESIGNATION": "${item.DESIGNATION}",
"EMPLOYMENT": "${item.EMPLOYMENT}",
"GENDER": "${item.GENDER}",
"Name1": "${item.Name1}",
"ROLE": "${item.ROLE}"
},`;
} else if (item.NODE == 'Devlopment') {
empData += `"GPH" : {
"DESIGNATION": "${item.DESIGNATION}",
"EMPLOYMENT": "${item.EMPLOYMENT}",
"GENDER": "${item.GENDER}",
"Name1": "${item.Name1}",
"ROLE": "${item.ROLE}"
}`;
}
}
});
empData += ']';
empData = JSON.parse(empData);
console.log(empData);
},