Using jq
or jtc
, I'd like to take a list of json files as inputs and generate a single json file based on the keys to the arrays found in each input file. Example:
1st file:
{
"key1": [
"key1-out1"
]
}
2nd file:
{
"key1": [
"key1-out2"
]
}
3rd file:
{
"key2": [
"key2-out1"
]
}
Expected output:
{
"key1": [
"key1-out1"
"key1-out2"
],
"key2": [
"key2-out1"
]
}
I encountered this almost perfectly good answer but I can't use it directly because I don't know what keys I will get in each input file.
CodePudding user response:
In case any of the files has more than one key:
jq -n 'reduce inputs as $in ({};
reduce ($in|keys_unsorted)[] as $k (.;
.[$k] = $in[$k]))' file1 file2 file3 # ...
You might want to tweak this so it doesn't fail if the value associated with a key is unexpectedly not an array.
And if you use gojq (the Go implementation of jq), you would use keys
rather than keys_unsorted
.