如何在 .dmc 文件中分组 JSON 个对象?
how Grouping JSON objects in .dmc file?
我有 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"
}
]
}
}
我想将 JSON 转换为按 id 分组,如下所示:
{
"sending": {
"field": [
{
"1234": [
{
"name": "Sample String",
"email": "Sample String"
},{
"name": "Sample String",
"email": "Sample String"
}
]
},
{
"1111": [
{
"name": "Sample String",
"email": "Sample String"
}
]
}
]
}
}
我在 .dmc 文件中编写了这段代码,但它无法正常工作:
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;
};
我使用 WSO2 数据映射器调解器并在集成工作室中将代码写入 .dmc 文件(数据映射器配置文件),有人可以帮忙吗?谢谢
你可以reduce
它。
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);
答案是
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);
我有 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"
}
]
}
}
我想将 JSON 转换为按 id 分组,如下所示:
{
"sending": {
"field": [
{
"1234": [
{
"name": "Sample String",
"email": "Sample String"
},{
"name": "Sample String",
"email": "Sample String"
}
]
},
{
"1111": [
{
"name": "Sample String",
"email": "Sample String"
}
]
}
]
}
}
我在 .dmc 文件中编写了这段代码,但它无法正常工作:
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;
};
我使用 WSO2 数据映射器调解器并在集成工作室中将代码写入 .dmc 文件(数据映射器配置文件),有人可以帮忙吗?谢谢
你可以reduce
它。
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);
答案是
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);