[英]How can I output a json with the new key name from the value in an existing json by jq
[英]Using jq how can I replace the name of a key with something else
這應該很容易...我想重命名幾個鍵(最好用 jq),但無論我做什么似乎都會出錯。 下面是一個 json 示例:
[
{
"fruit": "strawberry",
"veg": "apple",
"worker": "gardener"
}
]
我想將 veg 鍵重命名為fruit2(或例如,任何最簡單的)以及工作的工人鍵。
我意識到這在 sed 中是可能的,但我正在努力掌握 jq
使用以下jq方法:
jq '[.[] | .["fruit2"] = .veg | .["job"] = .worker | del(.veg, .worker)]' file
輸出:
[
{
"fruit": "strawberry",
"fruit2": "apple",
"job": "gardener"
}
]
關鍵 (:-) 是 with_entries。 例如,給定一個對象:
with_entries(if .key == "veg" then .key = "fruit2" else . end)
在您的情況下,由於您有一個對象數組,您可以將上述內容包裝在map( ... )
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.