Reputation: 66
I have couple of json String which I got using JSON.stringify
and now have to combine all jsonString in one single json.
below are the 3 json strings I have
json 1= '{
"authSeqNo" : 14,
"flagNewEdit": "EDIT",
"groupId": "AD0009",
"groupName": "IT-Updated",
"roleId": "Admin-Updated",
"recordStatus": "A"
}'
json 2=
{"userList": [
{
"userId": "x",
"email": "[email protected]",
"isDelete" : "TRUE"
},
{
"userId": "y",
"email": "[email protected]",
"isDelete" : "FALSE"
}
]}
json 3=
{"authMenuList": [
{
"menuId" : "ATHMGT",
"viewFlag": "1",
"createFlag": "1",
"editFlag": "0",
"deleteFlag": "1",
"creditnoteFlag": "0",
"cancelFlag": "1"
}]}
Now have to join all these 3 to 1, I have tried the below way
var completeDetails = json1.concat(json2);
completeDetails=completeDetails.concat(json3);
but it's not giving the desired output.
my expected result should be like below
{
"authSeqNo" : 14,
"flagNewEdit": "EDIT",
"groupId": "AD0009",
"groupName": "IT-Updated",
"roleId": "Admin-Updated",
"recordStatus": "A",
"userList": [
{
"userId": "x",
"email": "[email protected]",
"isDelete" : "TRUE"
},
{
"userId": "y",
"email": "[email protected]",
"isDelete" : "FALSE"
}
],
"authMenuList": [
{
"menuId" : "ATHMGT",
"viewFlag": "1",
"createFlag": "1",
"editFlag": "0",
"deleteFlag": "1",
"creditnoteFlag": "0",
"cancelFlag": "1"
}]
}
but I getting output is
{
"authSeqNo": "0",
"flagNewEdit": "NEW",
"groupId": "TEST",
"groupName": "GroupN",
"roleId": "Administrator",
"recordStatus": ""
} {
"userList": "[{"
userId ":"
x ","
email ":"
x @v.com ","
delete ":"
"}, {
"userId": "asdkl",
"email": "[email protected]",
"delete": ""
}]
"} {
"authMenuList[{"
menuId ":"
ATHMGT ","
viewFlag ":"
1 ","
createFlag ":"
1 ","
editFlag ":"
0 ","
deleteFlag ":"
1 ","
creditnoteFlag ":"
0 ","
cancelFlag ":"
1 "}]}
I am new to javascript and learning it. Please help me to solve this.
Upvotes: 2
Views: 15110
Reputation: 21658
const json1= `{
"authSeqNo" : 14,
"flagNewEdit": "EDIT",
"groupId": "AD0009",
"groupName": "IT-Updated",
"roleId": "Admin-Updated",
"recordStatus": "A"
}`
const json2=`
{"userList": [
{
"userId": "x",
"email": "[email protected]",
"isDelete" : "TRUE"
},
{
"userId": "y",
"email": "[email protected]",
"isDelete" : "FALSE"
}
]}`
const json3=`
{"authMenuList": [
{
"menuId" : "ATHMGT",
"viewFlag": "1",
"createFlag": "1",
"editFlag": "0",
"deleteFlag": "1",
"creditnoteFlag": "0",
"cancelFlag": "1"
}]}`
const extendJSON = (...objs) => objs.reduce((result, current) => ({...result, ...JSON.parse(current)}), {});
console.log(extendJSON(json1, json2, json3));
Upvotes: 0
Reputation: 617
If you would want to use jQuery, you can use $.extend
function to merge all your parsed json
strings into one single javascript object and stringify the result. Code sample below.
const object1 = JSON.parse(json_1);
const object2 = JSON.parse(json_2);
const object3 = JSON.parse(json_3);
const mergedObject = $.extend({}, object1, object2, object3);
const mergedJSON = JSON.stringify(mergedObject);
console.log(mergedJSON);
By passing an empty object as the target(first) argument, you can preserve both the objects. If however you want to merge the second and third object, you can do like $.extend(object2, object3).
See documentation here - jQuery.extend API
Upvotes: 0
Reputation: 519
You could convert them to JS objects and combine them
So:
const obj1 = JSON.parse(json1);
const obj2 = JSON.parse(json2);
const obj3 = JSON.parse(json3);
const mergedObj = Object.assign(obj1, obj2, obj3);
const jsonStr = JSON.stringify(mergedObj);
jsonStr should have the three JSONs combined
I just saw that you use JSON.stringify() to get your strings, so if you have three objects, just do the Object.assign() portion and you should be good.
Upvotes: 6