[英]Get list of values for each unique key of json with jq
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: json 看起来像:
{
"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, ...],
...
}
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"]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.