[英]jq parsing json input with variables
I have this input: 我有这个输入:
{
"users": [
{
"name": "tester-01",
"user": {
"username": "tester01"
}
},
{
"name": "tester-02",
"user": {
"username": "tester02"
}
}
],
"current-user": "tester-02"
}
Using jq (1.5), I want to print the username that matches current-user. 使用jq(1.5),我要打印与当前用户匹配的用户名。
Can anyone share how this would be done or tips to get me started? 任何人都可以分享如何完成操作或提示以帮助我入门吗?
This was my solution. 这是我的解决方案。
cat 99 | jq -r '."current-user" as $foo | .users[] | select(.name == $foo).user'
{
"username": "tester02"
}
The select solution is good. 选择解决方案是好的。 Here is an alternative that uses foreach .
这是使用foreach的替代方法。
foreach .users[] as $u (
."current-user"
; .
; if . == $u.name then $u.user.username else empty end
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.