I want to convert one below json format to respective json format. The Json I receive from Rest services is not supporting the design.I tired to implement the below list view
JSON Format (I receive from Rest services)
{
"ActionHistory": [{
"id": "action",
"name": "Action History",
"children": [{
"childrenItem": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
}, {
"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}]
},
{
"id": "action",
"name": "Action History2",
"children": [{
"childrenItem": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
}, {
"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}]
}
]
}
JSON format I need
{
"ActionHistory": [
{
"attr": {
"id": "action",
"name": "Action History"
},
"children": [{
"attr": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
},
{
"attr": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}
]
}, {
"attr": {
"id": "action",
"name": "Action History"
},
"children": [{
"attr": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To": "Neena Kochlar",
"pic": "deb_avatar",
"Details": ""
}
},
{
"attr": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To": "James",
"pic": "neena_avatar",
"Details": ""
}
}
]
}
]
}
I tired to convert the JSON format and it worked well for JSON array of length=1.For greater Array length it is not working. Here is my code https://jsfiddle.net/72mz5zft/
I think essentially what you're trying to do is pretty simple mapping:
var person={"ActionHistory":[
{
"id": "action",
"name": "Action History",
"children": [
{"childrenItem": {
"id": "action1",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 18-Apr-2017 18:20:32",
"Action": "Submit",
"From": "Deb Raphaely",
"To":"Neena Kochlar",
"pic":"deb_avatar",
"Details":""
}
},{"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "on 19-Apr-2017 18:20:32",
"Action": "Approve",
"From": "Neena Kochlar",
"To":"James",
"pic":"neena_avatar",
"Details":""
}
},
{"childrenItem": {
"id": "action2",
"type": "fa fa-info",
"empId": "101",
"ActionDate": "",
"Action": "Pending",
"From": "James",
"To":"",
"pic":"james_avatar",
"Details":""
}
}
]
}
]};
var result = {
ActionHistory: person.ActionHistory.map(function(item){
return {attr: {
id: item.id,
name: item.name,
children: item.children.map(function(child){
return {
attr: child.childrenItem
}
})
}}
})
};
console.log(result);