所以我有三個檔案:
cats.json
{}。
"cats": []。
{
"name": "fluffles",
"年齡": 10,
"color": "whit"
}
]
}
dogs.json
{
"dogs": []。
{
"name": "sam",
"age": 5,
"color": "Black and White"
},
{>
"name": "rover",
"age": 2,
"color": "brown and white"/span>
}
]
}
snakes.json
{
"蛇": []。
{
"name": "noodles",
"年齡": 10,
"color": "green"
}
]
}
我想把這些合并在一起,放在一個 "動物 "物件下。我發現這將會合并這些檔案:
jq -s '{"animals": .}' cats.jpeg. ' cats.json dogs.json snakes.json > animals.json
{}。
"動物": []。
{
"cats": [{
"name": "fluffles",
"年齡": 10,
"color": "whit"
}
]
},
{>
"dogs": []。
{
"name": "sam",
"age": 5,
"color": "Black and White"
},
{>
"name": "rover",
"age": 2,
"color": "brown and white"/span>
}
]
},
{>
"蛇": [/span>
{
"name": "noodles",
"年齡": 10,
"color": "green"
}
]
}
]
}
現在我有一個額外的物件:
owners.json
{
"owners": []。
"peter",
"william"/span>,
"sally" "sally"
]
}
我想用
將其合并到同一個檔案中。jq -s '.[0] .[1] ' animals.json owners.json
我可以只用一個jq命令來完成這兩個操作嗎?
結果將是這樣的:
{}。
"動物": []。
{
"cats": [{
"name": "fluffles",
"年齡": 10,
"color": "whit"
}
]
},
{>
"dogs": []。
{
"name": "sam",
"age": 5,
"color": "Black and White"
},
{>
"name": "rover",
"age": 2,
"color": "brown and white"/span>
}
]
},
{>
"蛇": [/span>
{
"name": "noodles",
"年齡": 10,
"color": "green"
}
]
}
],
"所有者": []。
"peter",
"william"/span>,
"sally" "sally"
]
}
uj5u.com熱心網友回復:
不確定這是否是正確的方法,但它通過以下方式獲得了所需的輸出:
--slurp:[ ...[0] * ...[1] * ...[2] ] 作為$all。
owners物件作為單個變數.
.[3].owners as $ownersjq
--slurp
'[ ...[0] * ...[1] * ...[2] ] as $all | ...[3].owners as $owners | { "動物": $all, "所有者": $owners }' cats.json dogs.json snakes.json owners.json
將產生:
{}。
"動物": []。
{
"cats":[
{
"name": "fluffles",
"年齡": 10,
"color": "whit"
}
],
"dogs": [/span>
{
"name": "sam",
"age": 5,
"color": "Black and White"
},
{>
"name": "rover",
"age": 2,
"color": "brown and white"/span>
}
],
"蛇": [/span>
{
"name": "noodles",
"年齡": 10,
"color": "green"
}
]
}
],
"所有者": []。
"peter",
"william"/span>,
"sally" "sally"
]
}
uj5u.com熱心網友回復:
假設你有一個(先驗的)不確定的或大量的動物型別,而只有一個owners檔案。 在這種情況下,最好(為了節省記憶體)不使用-s選項,將所有者檔案作為第一個資料檔案來呼叫jq會更容易,例如:
jq -n -f program.jq owners.json $(ls *.json | grep -v owners.json)
其中program.jq包含一個程式,例如:
input as $owners | {$owners, animals: [inputs]}
(注意{"owners": $owners}可以被縮寫。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/309908.html
標籤:
上一篇:Facebook永久用戶訪問令牌
