I have a requirement where I need to compare 2 input json objects - obj1 and obj2. Both inputs can have same keys as well as additional key.
- In case of same keys in both inputs, the values should be fetched from obj2.
- In case a key is not available in obj2, it should fetch both key and value from obj1.
- In case the key is not available in obj1, it should fetch both key and value from obj2.
Below is the sample inputs and expected output
Inputs:
obj1:
{
"id": "123",
"fname": "John",
"lname": "Sam",
"gender": "F",
"address1": {
"country": "USA",
"city": "San Jose",
"pin": null
},
"officeDetails": [
{
"workLocation": "Home"
}
]
}
obj2:
{
"id": "123",
"fname": "Victor",
"lname": "Sam",
"age": "11",
"gender": "",
"address1": {
"country": "USA",
"pin": 95112
},
"officeDetails": [
{
"laptop": "Y",
"mouse": "Y"
}
]
}
Expected Output:
{
"id": "123",
"fname": "Victor",
"lname": "Sam",
"age": "11",
"gender": "",
"address1": {
"country": "USA",
"city": "San Jose",
"pin": 95112
},
"officeDetails": [
{
"laptop": "Y",
"mouse": "Y",
"workLocation": "Home"
}
]
}
Thanks in advance