使用jq使用可变键构造JSON [英] Construct JSON with a variable key using jq
本文介绍了使用jq使用可变键构造JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用jq
构造一个哈希值,其中键名来自变量.像这样:
I am trying to use jq
to construct a hash in which a key name comes from a variable. Something like this:
jq --null-input --arg key foobar '{$key: "value"}'
但是,这不起作用,并出现以下错误:
This doesn't work, however, and gives the following error:
error: syntax error, unexpected '$'
{$key: "value"} 1 compile error
推荐答案
早在以下时间,使用括号就可以对$key
进行求值:
Use parentheses to evaluate $key
early as in:
jq --null-input --arg key foobar '{($key): "value"}'
另请参见: JQ中针对.key的括号
这篇关于使用jq使用可变键构造JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文