I want to merge two JSON objects that has a common key
Object A
{
"extensions": {
"app_name": "extensions-prod"
},
"plugins": {
"app_name": "plugins-prod"
}
}
Object B
{
"plugins": {
"project_name": "plugins-prod"
}
}
Desired output:
{
"plugins": {
"project_name": "plugins-prod",
"app_name": "plugins-prod"
}
}
I did look into other SO posts but were not very useful as the structure I have is different. Also the json objects are bash variables and not contained in a file.
[Edit] I would not know what the common key would be since the JSON objects are being generated dynamically in a CI environment.
2
Answers
One possible solution that is admittedly pedestrian but which avoids the generality and potential complexity of a "deep merge" would be:
If you want the ordering of keys shown in the Q, you could modify this to:
There are many variations of the above theme.
This should achieve what’s expected :