I have two sets of JSONs with me. One is a request JSON and another is response JSON. I need to compare request JSON with response JSON based on a certain field and if a match is found I need to merge the data from request and response json and if a match is not found the I still have merge the data but leave some fields blank/not found that are missing. I have created a code but I am getting duplicates when I am creating the final output structure. It will be really great if someone can help me fix the problem.
req_json:
{
"val": [
{
"custNumber": "Z10000",
"custID": "1000",
"custName": "Jack",
"custType": "Private"
},
{
"custNumber": "Z20000",
"custID": "2000",
"custName": "Tina",
"custType": "Private"
},
{
"custNumber": "B31000",
"custID": "3100",
"custName": "ACME Holdings",
"custType": "Business"
}
]
}
resp_json:
{
"SELECT_FROM_DB_response": {
"row": [
{
"CUSTNO": "Z10000",
"PRODUCTID": "P21000",
"PRODUCTNAME": "KEYBOARD",
"PRODUCTSTATUS": "Shipped",
},
{
"CUSTNO": "Z20000",
"PRODUCTID": "L53000",
"PRODUCTNAME": "MOUSE",
"PRODUCTSTATUS": "Processing",
},
{
"CUSTNO": "B31000",
"PRODUCTID": "N99500",
"PRODUCTNAME": "MONITOR",
"PRODUCTSTATUS": "Delivered",
}
]
}
}
My code is something as shown below. However, when I am executing it, I am getting duplicates as output. I need to fix this problem but so far I had no luck.
GROOVY CODE:
def mReqOrders = new JsonSlurper().parseText(req_json)
def mRespOrders = new JsonSlurper().parseText(resp_json)
Set finalJsonSet = []
mReqOrders.val.each { reqOrder ->
mRespOrders.SELECT_FROM_DB_response.row.each { respOrder ->
if (reqOrder.custNumber == respOrder.CUSTNO) {
finalJsonSet << [
order_custNumber: respOrder.CUSTNO,
order_custID: reqOrder.custID,
order_custName: reqOrder.custName,
order_custType: reqOrder.custType,
order_productID: respOrder.PRODUCTID,
order_productNAME: respOrder.PRODUCTNAME,
order_productSTATUS: respOrder.PRODUCTSTATUS
]
}
else {
finalJsonSet << [
order_custNumber: respOrder.CUSTNO,
order_custID: reqOrder.custID,
order_custName: reqOrder.custName,
order_custType: reqOrder.custType,
order_productID: "not found",
order_productNAME: "not found",
order_productSTATUS: "not found"
]
}
}
}
//Convert the Set to JSON Format
finalJson = JsonOutput.prettyPrint(JsonOutput.toJson(finalJsonSet))
println (finalJson)
CURRENT OUTPUT:
[
{
"order_custNumber": "Z10000",
"order_custID": "1000",
"order_custName": "Jack",
"order_custType": "Private",
"order_productID": "P21000",
"order_productNAME": "KEYBOARD",
"order_productSTATUS": "Shipped"
},
{
"order_custNumber": "Z20000",
"order_custID": "1000",
"order_custName": "Jack",
"order_custType": "Private",
"order_productID": "not found",
"order_productNAME": "not found",
"order_productSTATUS": "not found"
},
{
"order_custNumber": "B31000",
"order_custID": "1000",
"order_custName": "Jack",
"order_custType": "Private",
"order_productID": "not found",
"order_productNAME": "not found",
"order_productSTATUS": "not found"
},
{
"order_custNumber": "Z10000",
"order_custID": "2000",
"order_custName": "Tina",
"order_custType": "Private",
"order_productID": "not found",
"order_productNAME": "not found",
"order_productSTATUS": "not found"
},
{
"order_custNumber": "Z20000",
"order_custID": "2000",
"order_custName": "Tina",
"order_custType": "Private",
"order_productID": "L53000",
"order_productNAME": "MOUSE",
"order_productSTATUS": "Processing"
},
{
"order_custNumber": "B31000",
"order_custID": "2000",
"order_custName": "Tina",
"order_custType": "Private",
"order_productID": "not found",
"order_productNAME": "not found",
"order_productSTATUS": "not found"
},
{
"order_custNumber": "Z10000",
"order_custID": "3100",
"order_custName": "ACME Holdings",
"order_custType": "Business",
"order_productID": "not found",
"order_productNAME": "not found",
"order_productSTATUS": "not found"
},
{
"order_custNumber": "Z20000",
"order_custID": "3100",
"order_custName": "ACME Holdings",
"order_custType": "Business",
"order_productID": "not found",
"order_productNAME": "not found",
"order_productSTATUS": "not found"
},
{
"order_custNumber": "B31000",
"order_custID": "3100",
"order_custName": "ACME Holdings",
"order_custType": "Business",
"order_productID": "N99500",
"order_productNAME": "MONITOR",
"order_productSTATUS": "Delivered"
}
]
EXPECTED OUTPUT:
[
{
"order_custNumber": "Z10000",
"order_custID": "1000",
"order_custName": "Jack",
"order_custType": "Private",
"order_productID": "P21000",
"order_productNAME": "KEYBOARD",
"order_productSTATUS": "Shipped"
},
{
"order_custNumber": "Z20000",
"order_custID": "2000",
"order_custName": "Tina",
"order_custType": "Private",
"order_productID": "L53000",
"order_productNAME": "MOUSE",
"order_productSTATUS": "Processing"
},
{
"order_custNumber": "B31000",
"order_custID": "3100",
"order_custName": "ACME Holdings",
"order_custType": "Business",
"order_productID": "N99500",
"order_productNAME": "MONITOR",
"order_productSTATUS": "Delivered"
}
]