Multiplying two objects together will merge them. The following uses reduce to successively build up a new object by merging the existing objects one by one.
Two similar ways to do this:
Using
-nandinputswithreduce:jq -n 'reduce inputs as $item ({}; . *= $item)' fileThis uses the
inputsfunction, which returns the incoming object,objects one by one. This is combined with-n(--null-input) to disable thothe ordinary reading of inputs.Using
-sand.[]withreduce:jq -s 'reduce .[] as $item ({}; . *= $item)' fileThis reads the original data with
-s(--slurp) into an array to start with, andreduceis used to iterate over that array. Note that this is severely memory-inefficient and slow if the input is huge.
The result, given your data (and correcting it by removing a comma on the createat line):
{
"daisy": {
"dev": {
"app_url": "example.com",
"owner": "bob",
"createat": "2022-06-17T15:31:58Z",
"appname": "daisy",
"flavor": [
"nodejs"
],
"file": "manifests/daisy.yml"
}
}
}
Note that if two or more objects share the same key and if that key refers to a scalar or array, then the later objects in the input will overwrite the value.