guys. There's the task: to get all unique keys and lists of matching values for these keys and then wrap them in {}
The json looks like:
{
"sepalLength": 5,
"sepalWidth": 3.3,
"petalLength": 1.4,
"petalWidth": 0.2,
"species": "setosa"
},
{
"sepalLength": 7,
"sepalWidth": 3.2,
"petalLength": 4.7,
"petalWidth": 1.4,
"species": "versicolor"
},
{
"sepalLength": 6.4,
"sepalWidth": 3.2,
"petalLength": 4.5,
"petalWidth": 1.5,
"species": "versicolor"
},
...
And the result must look this way:
{
"species": ["setosa", "setosa", ...],
"petalWidth": [1.2, ...],
...
}
CodePudding user response:
Assuming, your input data is an array
[
{
"sepalLength": 5,
"sepalWidth": 3.3,
"petalLength": 1.4,
"petalWidth": 0.2,
"species": "setosa"
},
{
"sepalLength": 7,
"sepalWidth": 3.2,
"petalLength": 4.7,
"petalWidth": 1.4,
"species": "versicolor"
},
...
]
The following
jq '[(map(keys[]) | unique[]) as $key | {($key): map(.[$key])} ] | add'
if fed with your sample data will produce something like
{
"petalLength": [1.4,4.7,4.5],
"petalWidth": [0.2,1.4,1.5],
"sepalLength": [5,7,6.4],
"sepalWidth": [3.3,3.2,3.2],
"species": ["setosa","versicolor","versicolor"]
}