joelvh / json2json

Transform (reformat) JSON structures from one to another using JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array content of source json not merged with another object

kameshkannan opened this issue · comments

I have requirement that i need to merge the array content of source json and one class as target. As given below

Target:
var dest = {
one: 2,
two: 3,
bank: "bankname",
code: "xyz123455"
};

Source:
var source = {
"src": {
"items": [
{ "name": "one", "rank": 1 },
{ "name": "two", "rank": 2 }
],
"credit": {
"bank": "bankname",
"code": "xyz123455"
}
}
};

Template is:

tmpl = {
path: '.',
aggregate: {
total: function (key, value, existing) {
if (!isArray(value)) {
return value;
} else {
return value.sort().reverse()[0];
}
},
pages: function (key, value, existing) {
if (!isArray(value)) {
return value;
} else {
return value.sort().reverse()[0];
}
}
},
as: {
details: {
path: 'src.items',
key: 'name',
value: 'rank',
aggregate: function (key, value, existing) {
return Math.max(value, existing || 0)
},

    },
    bank: 'src.credit.bank',
    code: 'src.credit.code'
}

};

But it returns the Response/Destination object as

{
"details": {
"one": 1,
"two": 2
},
"bank": "bankname",
"code": "xyz123455"
}

@kameshkannan it's been a while since I used this package -- any luck finding a solution?

Luckily the requirement changed hence we did not face the problem as i mentioned earlier, it is good that we leveraged other features which are exists. Thanks.

@kameshkannan great to hear - I would imagine a custom filter/function would do the trick. Closing.