Is there a way to effectively merge JSON objects in such a manner that simple values (strings, numbers, booleans, etc) get overridden when keys match, but when dealing with complex values (arrays and objects), different outcomes apply? 1.) For simple arrays of strings, the strings should be merged, while for objects, a recursive solution should be implemented. 2.) When dealing with objects, again, a recursive solution is necessary.
I have experimented with various npm packages, but I am still on the lookout for the ideal solution.
const jsonA = {
Results: [
{
PgId: "pg1",
Entities: [
{
EntityName: "Customer",
Subjects: [
{
first_name: "Mark",
last_name: "woodruff",
location: "tenino"
},
{
first_name: "helen1",
last_name: "mclean1",
location: "washington1"
}
]
}
]
}
],
pets: ['Cat', 'Parrot'],
isComplete: false
}
const jsonB = {
Results: [
{
PgId: "pg1",
Entities: [
{
EntityName: "Customer",
Subjects: [
{
first_name: "Mark",
last_name: "woodruff",
location: "tenino"
},
{
first_name: "helen",
last_name: "mclean",
location: "washington"
}
]
}
]
}
],
pets: ['Dog'],
isComplete: true
}
EXPECTED OUTPUT :
const mergedBA_B_overrides_A = {
Results: [
{
PgId: "pg1",
Entities: [
{
EntityName: "Customer",
Subjects: [
{
first_name: "Mark",
last_name: "woodruff",
location: "tenino"
},
{
first_name: "helen",
last_name: "mclean",
location: "washington"
},
{
first_name: "helen1",
last_name: "mclean1",
location: "washington"
}
]
}
]
}
],
pets: ['Dog','Cat', 'Parrot'],
isComplete: true
}