I have this sample of JSON :
{
"sending": {
"field": [
{
"email": "Sample String",
"id": 1234,
"name": "Sample String"
}, {
"email": "Sample String",
"id": 1234,
"name": "Sample String"
},{
"email": "Sample String",
"id": 1111,
"name": "Sample String"
}
]
}
}
I want to converted JSON to be grouping by id like this:
{
"sending": {
"field": [
{
"1234": [
{
"name": "Sample String",
"email": "Sample String"
},{
"name": "Sample String",
"email": "Sample String"
}
]
},
{
"1111": [
{
"name": "Sample String",
"email": "Sample String"
}
]
}
]
}
}
I wrote this code in .dmc file but it does not work as I want :
map_S_root_S_root = function(){
var outputroot={};
var a = 0;
outputroot = {};
outputroot.sending = {};
outputroot.sending.field = [];
for(i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491 in inputroot.sending.field){
outputroot.sending.field[a] = {};
id = inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].id;
outputroot.sending.field[a].id = [];
var c =0;
for(i in outputroot.sending.field[c].id){
if (inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].id === outputroot.sending.field[c].id){
outputroot.sending.field[c].id[c] += inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].name;
}
c++
}
outputroot.sending.field[a].id[a] = {};
outputroot.sending.field[a].id[a].name = inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].name;
outputroot.sending.field[a].id[a].email = inputroot.sending.field[i_field_1d3104f4_e7ee_449b_9653_ccc4f8985491].email;
a++;
}
return outputroot;
};
I use WSO2 data mapper mediator and write the code in .dmc file (data mapper configuration file ) in integration studio , could anyone help? Thanks
The answer is
result = {
"field": [{
"id": "11",
"name": "asma",
"email": "asma@hotmail"
}, {
"id": "11",
"name": "jone",
"email": "jone@hotmail"
}, {
"id": "1234",
"name": "jak",
"email": "jak@hotmail"
}]
}
results = result.field;
groups = {};
for (var i in results) {
var groupName = results[i].id;
if (!groups[results[i].id]) {
groups[groupName] = [];
}
groups[groupName].push({"name" :results[i].name,"email":results[i].email})
}
console.log(groups);