- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 03:45 AM - edited 03-10-2023 03:56 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 07:02 AM
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**
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 03:58 AM
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**
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 06:32 AM
Hi @Hemant Goldar thanks for your quick reply ,am have taken your ref and written below code .but not getting the merge json
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 07:02 AM
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**
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2023 07:54 AM
thanks @Hemant Goldar it works !!