Home > Software engineering >  Merge 2 JSON(Object Array) Data Restructuring
Merge 2 JSON(Object Array) Data Restructuring

Time:07-01

Can anyone help us, we have been on this for ages. Basically, we want to merge 2 JSON responses dynamically by using JSON Data 2( prodsub_id) data with JSON Data 1(id). Please see below sample JSON data.

JSON Data 1

[ { "enabled": 1, "id": 1, "parent_screen": "Products", "category_title": "Regular Savings Account", "category_desc": "Regular Savings Account", "category_imgpath": "wdbwebsite/Products/RegSavings/Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg", "category_imgpath_filename": "Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg", "features": "Low maintaining balance\nInterest credited\nTransact conveniently at any WealthBank branch even on weekends with no Interbranch Service Fee", "requirement": "wdbwebsite/Products/RegSavings/Products-Regular_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "requirement_filename": "Products-Regular_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "contactus_desc": null, "retVal": 1, "errmsg": "Data found" }, { "enabled": null, "id": 11, "parent_screen": "Products", "category_title": "Special Savings Account", "category_desc": "Special Savings Account", "category_imgpath": "wdbwebsite/Products/SpecialSavings/Products-Special_Savings_Account-img-2.jpg", "category_imgpath_filename": "Products-Special_Savings_Account-img-2.jpg", "features": "Interest shall be credited upon maturity of the placement/term\nOption to roll-over the placement or top-up", "requirement": "wdbwebsite/Products/SpecialSavings/Products-Special_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "requirement_filename": "Products-Special_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "contactus_desc": null, "retVal": 1, "errmsg": "Data found" } ]

JSON DATA 2

[ { prodsub_id: 11, parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content', offer: 'offer1' }, { prodsub_id: 11, parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content', offer: 'offer2' }, { prodsub_id: 11, parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content', offer: null }, { prodsub_id: 10, parent_screen: 'Products', narra_title: 'Nara title', narra_content: 'nara content', offer: null } ]

So to simplify it all. We need to have this output.

JSON FINAL OUTPUT

{ "parent_screen": "Products", "category_imgpath": "", "narrative": [ { "title": "qwe", "content": "qwe" }, { "title": "w", "content": "qwer" }, { "title": "qwer", "content": "asdf" } ], "category_title": "Regular Savings Account", "category_desc": "qwer", "offer": [ "qwer", "wqer", "qwerwe" ], "requirement": "", "req_img": "", "link": "regularsavingsaccount", "features": "asdf", "enabled": 1, "id": 11, "img": "" }

Since the 2nd JSON data has a prodsub_id that is existing in the 1st JSON data (id), We would like to insert some 2nd JSON data into the 1st JSON data, as shown in JSON FINAL OUTPUT. Sorry for my bad English, any help will be greatly appreciated.

CodePudding user response:

The easiest way I can think to do this (which may not be the best), would be to loop over the items in JSON 1 and then do a filter on JSON 2 to return any items with prodsub_id equal to the id of the current item from JSON 1 and then just add whatever fields you need from the matching item in JSON 2 (if one exists) to the current item from JSON 1.

Something like:

json1.forEach(item => {
  const matched = json2.filter(i => i.prodsub_id == item.id);
  if(matched.length){
    // add stuff from matched to item
  }
});

Edit: If there is more than 1 match (matched will always be an array), you'll need to add logic inside the if to loop over each item that was matched and add whatever you need to item.

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter for info about the filter function.

CodePudding user response:

you can do something like this using 2 reduce in cascade

const mergeArrays = (ar1, key1, ar2, key2) => Object.values(
ar2.reduce((res, item) => {
 const key = item[key2]
 return {
   ...res,
   [key]: {
     ...(res[key] || {}),
     ...item
    }
 }

}, ar1.reduce((res, item) => {
 const key = item[key1]
 return {
   ...res,
   [key]: item
 }
}, {})))


const data1 = [ { "enabled": 1, "id": 1, "parent_screen": "Products", "category_title": "Regular Savings Account", "category_desc": "Regular Savings Account", "category_imgpath": "wdbwebsite/Products/RegSavings/Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg", "category_imgpath_filename": "Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-Products-Regular_Savings_Account-ggggg.jpg", "features": "Low maintaining balance\nInterest credited\nTransact conveniently at any WealthBank branch even on weekends with no Interbranch Service Fee", "requirement": "wdbwebsite/Products/RegSavings/Products-Regular_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "requirement_filename": "Products-Regular_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "contactus_desc": null, "retVal": 1, "errmsg": "Data found" }, { "enabled": null, "id": 11, "parent_screen": "Products", "category_title": "Special Savings Account", "category_desc": "Special Savings Account", "category_imgpath": "wdbwebsite/Products/SpecialSavings/Products-Special_Savings_Account-img-2.jpg", "category_imgpath_filename": "Products-Special_Savings_Account-img-2.jpg", "features": "Interest shall be credited upon maturity of the placement/term\nOption to roll-over the placement or top-up", "requirement": "wdbwebsite/Products/SpecialSavings/Products-Special_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "requirement_filename": "Products-Special_Savings_Account-Requirement-Products-Regular_Savings_Account-Requirement-dtr0215.pdf", "contactus_desc": null, "retVal": 1, "errmsg": "Data found" } ]

const data2 = [ { prodsub_id: 11, parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content', offer: 'offer1' }, { prodsub_id: 11, parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content', offer: 'offer2' }, { prodsub_id: 11, parent_screen: 'Products',
narra_title: 'Nara title',
narra_content: 'nara content', offer: null }, { prodsub_id: 10, parent_screen: 'Products', narra_title: 'Nara title', narra_content: 'nara content', offer: null } ]

const result = mergeArrays(data1, 'id', data2, 'prodsub_id')

console.log(result)

  • Related