Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

how to merg two jsons into single json with primary key

String
Kilo Sage

Hi Team ,

we are reaching the two jsons and we need to merge into one json depends on primary key 

 

Json1=

[{"asset_details":"0f19a6518787d11089e3c8490cbb35bb","u_order":"1","chargeable":"true","amount":"","shipto_loc":"Finland","IO:49eb58f81ba9691009952f02604bcbd9":"false","total_qty":"","u_model_number":"3766B002","u_consumable":"4c61ce9e87d4a51089e3c8490cbb350b","u_price":"","product_name":"ab775db51b31951009952f02604bcbeb","max_qty":""},{"asset_details":"0f19a6518787d11089e3c8490cbb35bb","u_order":"11","chargeable":"true","amount":"","IO:6eebd0f81ba9691009952f02604bcbcc":"false","shipto_loc":"Finland","total_qty":"","u_model_number":"3766B","u_consumable":"4c61ce9e87d4a51089e3c8490cbb350b","u_price":"","product_name":"ab775db51b31951009952f02604bcbeb","max_qty":""}]

 

Json2=

[{"u_model_number":"3766B002","amount":"70”},{“u_model_number":"3766B","amount":"75"}]

 

Primary key is u_model_number in both the json

 

expected JSON as output :

 

[{"asset_details":"0f19a6518787d11089e3c8490cbb35bb","u_order":"1","chargeable":"true","amount":"70","shipto_loc":"Finland","IO:49eb58f81ba9691009952f02604bcbd9":"false","total_qty":"","u_model_number":"3766B002","u_consumable":"4c61ce9e87d4a51089e3c8490cbb350b","u_price":"","product_name":"ab775db51b31951009952f02604bcbeb","max_qty":""},{"asset_details":"0f19a6518787d11089e3c8490cbb35bb","u_order":"11","chargeable":"true","amount”:”75”,”IO:6eebd0f81ba9691009952f02604bcbcc":"false","shipto_loc":"Finland","total_qty":"","u_model_number":"3766B","u_consumable":"4c61ce9e87d4a51089e3c8490cbb350b","u_price":"","product_name":"ab775db51b31951009952f02604bcbeb","max_qty":""}]

 

 

we have to update the amount field with respective u_model_number(primary key)

 

Please guide me 

 

 

 

1 ACCEPTED SOLUTION

Hi @String,

 

try this code

var json1=mrvsjson;
var json2=inboundjson;
 
for (var i = 0; i < mrvsjson.length; i++) {
  for (var j = 0; j < inboundjson.length; j++) {
    if (mrvsjson[i].u_model_number === inboundjson[j].u_model_number) {
      mrvsjson[i].amount = inboundjson[j].amount;
    }
  }
}

gs.info("final check"+JSON.stringify(mrvsjson));

 

I hope this helps!

 

Regards,

Hemant 

**Please mark my answer correct or helpful based on the impact**

View solution in original post

4 REPLIES 4

Hemant Goldar
Mega Sage

Hi @String,

 

Here is a sample code(not tested)

 

var json1 = [{"asset_details":"0f19a6518787d11089e3c8490cbb35bb","u_order":"1","chargeable":"true","amount":"","shipto_loc":"Finland","IO:49eb58f81ba9691009952f02604bcbd9":"false","total_qty":"","u_model_number":"3766B002","u_consumable":"4c61ce9e87d4a51089e3c8490cbb350b","u_price":"","product_name":"ab775db51b31951009952f02604bcbeb","max_qty":""},{"asset_details":"0f19a6518787d11089e3c8490cbb35bb","u_order":"11","chargeable":"true","amount":"","IO:6eebd0f81ba9691009952f02604bcbcc":"false","shipto_loc":"Finland","total_qty":"","u_model_number":"3766B","u_consumable":"4c61ce9e87d4a51089e3c8490cbb350b","u_price":"","product_name":"ab775db51b31951009952f02604bcbeb","max_qty":""}];
var json2 = [{"u_model_number":"3766B002","amount":"70"},{"u_model_number":"3766B","amount":"75"}];

var json1Map = {};
for (var i = 0; i < json1.length; i++) {
  json1Map[json1[i].u_model_number] = json1[i];
}

for (var i = 0; i < json2.length; i++) {
  var key = json2[i].u_model_number;
  if (json1Map[key]) {
    json1Map[key].amount = json2[i].amount;
  }
}

var mergedJson = [];
for (var key in json1Map) {
  mergedJson.push(json1Map[key]);
}

gs.print(mergedJson) // result

 

I hope this helps!

 

Regards,

Hemant 

**Please mark my answer correct or helpful based on the impact**

Hi @Hemant Goldar  thanks for your quick reply ,am have taken your ref and written below code .but not getting the merge json 

 

var json1=mrvsjson;
var json2=inboundjson;
 
var json1Map = {};
for (var i = 0; i < json1.length; i++) {
  json1Map[json1[i].u_model_number] = json1[i];
}
 
for (var j = 0; j < json2.length; j++) {
  var key = json2[j].u_model_number;
  if (json1Map[key]) {
    json1Map[key].amount = json2[j].amount;
 json1Map[key]. u_price = json2[j].price;
  }
}
 
Please guid me 
 
var mergedJson = [];
for (var key1 in json1Map) {
  mergedJson.push(json1Map[key1]);
}
 
gs.info("final check"+JSON.stringify(mergedJson));
        
 
        return JSON.stringify(mergedJson);
    },

Hi @String,

 

try this code

var json1=mrvsjson;
var json2=inboundjson;
 
for (var i = 0; i < mrvsjson.length; i++) {
  for (var j = 0; j < inboundjson.length; j++) {
    if (mrvsjson[i].u_model_number === inboundjson[j].u_model_number) {
      mrvsjson[i].amount = inboundjson[j].amount;
    }
  }
}

gs.info("final check"+JSON.stringify(mrvsjson));

 

I hope this helps!

 

Regards,

Hemant 

**Please mark my answer correct or helpful based on the impact**

thanks @Hemant Goldar  it works !!