I have a requirement where I have update the value of object of array based on the the values of object of another array. Below are Input arrays and the expected Output. Values of FieldName2 and FieldName3 in Input 2 are updated with the Value of "value" in Input 1 where ids are matching.
Input 1 :
[
{
"id": 123,
"key": "Dummy1",
"value": "20"
},
{
"id": 123,
"key": "Dummy2",
"value": "50"
},
{
"id": 123,
"key": "Dummy3",
"value": "100"
},
{
"id": 789,
"key": "Dummy2",
"value": "40"
},
{
"id": 789,
"key": "Dummy3",
"value": "90"
}
]
Input 2 :
[
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "Dummy1",
"FieldName3": "0"
},
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "Dummy1",
"FieldName3": "Dummy2"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "Dummy1",
"FieldName3": "0"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "Dummy1",
"FieldName3": "Dummy2"
}
]
Output :
[
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "20",
"FieldName3": "0"
},
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "20",
"FieldName3": "50"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "10",
"FieldName3": "0"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "10",
"FieldName3": "40"
}
]
CodePudding user response:
You can do something like this.
Script
%dw 2.0
output application/json
var inp1 = [
{
"id": 123,
"key": "Dummy1",
"value": "20"
},
{
"id": 123,
"key": "Dummy2",
"value": "50"
},
{
"id": 123,
"key": "Dummy3",
"value": "100"
},
{
"id": 789,
"key": "Dummy2",
"value": "40"
},
{
"id": 789,
"key": "Dummy3",
"value": "90"
}
]
var inp2 = [
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "Dummy1",
"FieldName3": "0"
},
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "Dummy1",
"FieldName3": "Dummy2"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "Dummy1",
"FieldName3": "0"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "Dummy1",
"FieldName3": "Dummy2"
}
]
var inpGrouped =inp1 groupBy ($.id $."key")
---
inp2 map {
"id":$.id ,
"FieldName": $.FieldName,
"FieldValue": $.FieldValue ,
"FieldName2": if(inpGrouped[($.id $.FieldName2)].value[0] != null) inpGrouped[($.id $.FieldName2)].value[0] else $.FieldName2,
"FieldName3": if(inpGrouped[($.id $.FieldName3)].value[0] != null) inpGrouped[($.id $.FieldName3)].value[0] else $.FieldName3
}
output
[
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "20",
"FieldName3": "0"
},
{
"id": 123,
"FieldName": "Dummy1",
"FieldValue": "20",
"FieldName2": "20",
"FieldName3": "50"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "Dummy1",
"FieldName3": "0"
},
{
"id": 789,
"FieldName": "Dummy1",
"FieldValue": "10",
"FieldName2": "Dummy1",
"FieldName3": "40"
}
]
PS: In this output corresponding to id 789
there is no value in the input (inp1 ) for "Dummy1". As a result you would seed the output as "Dummy1" as well.