如何在JQ中添加变量
我正在尝试在从JSON文件中获取信息时添加一个变量,如下所示。
stack=$(cat profiles.json | jq '.generic.category')
email=$(cat profiles.json | jq '.central.[Need to add $stack variable here].email')
echo $email
password=$(cat profiles.json | jq '.central.[Need to add $stack variable here].password')
echo $password
我尝试了一些事情,例如jq -arg v $ stack'.central [$ v] password*'
,但它不起作用。
的样子。
"central": {
"one": {
"tenant": "xxx-yyy-zzz",
"email": "[email protected]",
"password": "1111"
},
"two": {
"tenant": "aaa-bbb-ccc",
"email": "[email protected]",
"password": "2222"
}
},
"generic": {
"username": "root",
"password": "xyz",
"project": "ABC",
"category": "two"
}
这就是我的个人资料
I'm trying to add a variable while getting the info from a JSON file as shown below.
stack=$(cat profiles.json | jq '.generic.category')
email=$(cat profiles.json | jq '.central.[Need to add $stack variable here].email')
echo $email
password=$(cat profiles.json | jq '.central.[Need to add $stack variable here].password')
echo $password
I tried few things like jq --arg v $stack '.central[$v]password*'
but it didnt work.
This is what my the profiles.json looks like:
"central": {
"one": {
"tenant": "xxx-yyy-zzz",
"email": "[email protected]",
"password": "1111"
},
"two": {
"tenant": "aaa-bbb-ccc",
"email": "[email protected]",
"password": "2222"
}
},
"generic": {
"username": "root",
"password": "xyz",
"project": "ABC",
"category": "two"
}
What is the right command to fetch the required information using the variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在没有Shell变量
stack
的情况下执行,除非您想在Shell脚本的以后在不同的位置使用它。You can do without the shell variable
stack
, unless you want to use it in different places later in your shell script.