伙計們。任務是:獲取所有唯一鍵和這些鍵的匹配值串列,然后將它們包裝在 {} 中
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"
},
...
結果必須是這樣的:
{
"species": ["setosa", "setosa", ...],
"petalWidth": [1.2, ...],
...
}
uj5u.com熱心網友回復:
假設您的輸入資料是一個陣列
[
{
"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"
},
...
]
下列
jq '[(map(keys[]) | unique[]) as $key | {($key): map(.[$key])} ] | add'
如果用你的樣本資料喂食會產生類似的東西
{
"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"]
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324331.html
上一篇:如何在go中編組嵌入式結構?
