[英]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.