Input: The output from my current jq filters looks like
{
"id1": {
"version": 4,
"lastModifier": "abc"
}
}
{
"id2": {
"version": 3,
"lastModifier": "def"
}
}
{
"id3": {
"version": 8,
"lastModifier": "abc"
}
}
Desired Output: I just need to wrap this list in a object so I end up with
{
"ref": {
"id1": {
"version": 4,
"lastModifier": "abc"
},
"id2": {
"version": 3,
"lastModifier": "def"
},
"id3": {
"version": 8,
"lastModifier": "abc"
}
}
}
I don't want to merge anything from the input list into the output object, all the properties need to remain as-is, so piping to add
doesn't work. Also tried wrapping my filters with { "ref": (
filters )}
but that yields the same list with a "ref" wrapper for each object. Possibly reduce
is what is needed but haven't been able to construct that appropriately.
CodePudding user response:
Use the -s
flag to read the stream into an array, the add
it up and put it into your object:
jq -s '{ref: add}'
{
"ref": {
"id1": {
"version": 4,
"lastModifier": "abc"
},
"id2": {
"version": 3,
"lastModifier": "def"
},
"id3": {
"version": 8,
"lastModifier": "abc"
}
}
}
Of course, there is also a solution using reduce
:
jq -n 'reduce inputs as $in ({}; .ref = $in)'
{
"ref": {
"id1": {
"version": 4,
"lastModifier": "abc"
},
"id2": {
"version": 3,
"lastModifier": "def"
},
"id3": {
"version": 8,
"lastModifier": "abc"
}
}
}