How to create dynamic JSON as per input. Filter data and create appropriate JSON objects key and values pairs
Below is the database.
Below is the code which had tried but won't work...
success: function (data) {
//lenght
data.value.length
// console.log(data);
//HC list JSON
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);
//HC list JSON END
},
Something like this should work
function (data) {
//create array to store emp objects
var empData = new Array();
$.each(data.value, function (index, item) {
if (item.STATUS == 'Active') {
if (item.NODE == 'Testing') {
//create custom object
var emp = {
DDM_CO2: {
DESIGNATION: item.DESIGNATION,
EMPLOYMENT: item.EMPLOYMENT
//etc: item.etc
//etc: item.etc
//etc: item.etc
}
};
//add to array
empData.push(emp);
} else if (item.NODE == 'Devlopment') {
var emp = {
GPH: {
DESIGNATION: item.DESIGNATION,
EMPLOYMENT: item.EMPLOYMENT,
GENDER: item.GENDER,
Name1: item.Name1,
ROLE: item.ROLE
}
};
empData.push(emp);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.