本文介绍了JQ中.key的括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不明白为什么键必须在()
中才能起作用:
I don't understand why key has to be in ()
for this to work:
# kbrandt at glade.local in ~ on git:master x [15:08:19]
$ cat host | jq '. | to_entries | map({ (.key) : .value.CPU.PercentUsed })' | tail
{
"rpi-b827eb2d7d23": 10.333333333333334
},
{
"rpi-b827eb8d7c8d": 60
},
{
"rpi-b827eba999fa": 40.733333333333334
}
]
# kbrandt at glade.local in ~ on git:master x [15:08:54]
$ cat host | jq '. | to_entries | map({ .key : .value.CPU.PercentUsed })' | tail
jq: error: syntax error, unexpected FIELD (Unix shell quoting issues?) at <top-level>, line 1:
. | to_entries | map({ .key : .value.CPU.PercentUsed })
jq: 1 compile error
推荐答案
定义对象文字时,括号表示表达式值应为属性名称.否则,如果您不使用括号,则为文字名称.
When defining an object literal, the parentheses indicates that the expression value should be the property name. Otherwise if you didn't use parentheses, it's the literal name.
因此,这些是使用"foo"
属性定义对象的等效方法:
So these are equivalent ways to define an object with a "foo"
property:
{ foo: 1 }
{ "foo": 2 }
"foo" as $name | { ($name): 3 }
{ somename: "foo" } | { (.somename): 4 }
这篇关于JQ中.key的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!