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
You can reduce
it.
var array={ "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" } ] }}; array.sending.field = array.sending.field.reduce((acc,{id, ...rest})=>{ acc[id] =acc[id] || []; acc[id].push(rest); return acc; },{}); console.log(array);
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);
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.