I need to add or update values in multiple arrays without removing existed array values. Also I need to create objects If they did not exist.
somefile.json:
{ "messages": { "[email protected]": { "receivers": [ 1, 2, 3 ] }, "[email protected]": { "receivers": [ 7, 8, 9 ] } } } What I'm trying:
$ new='{"[email protected]":{"receivers":[1,2,3]},"[email protected]":{"receivers":[4,5,6]}}' $ jq --argjson data "$new" '.messages +=$data' somefile.json { "messages": { "[email protected]": { "receivers": [ 1, 2, 3 ] }, "[email protected]": { "receivers": [ 1, 2, 3 ] }, "[email protected]": { "receivers": [ 4, 5, 6 ] } } } Expected output:
{ "messages": { "[email protected]": { "receivers": [ 1, 2, 3 ] }, "[email protected]": { "receivers": [ 1, 2, 3, 7, 8, 9 ] }, "[email protected]": { "receivers": [ 4, 5, 6 ] } } }