我有一个 json 文件,大致如下所示:
{
"default": [
{
"name" : "Joe Bloggs",
"email" : "[email protected] /cdn-cgi/l/email-protection"
}
],
"groups": [
{
"recipients" : [
{
"name" : "Jane Bloggs",
"email" : "[email protected] /cdn-cgi/l/email-protection"
}
],
"orgs" : [
"Service A",
"Service B",
"Service C"
]
},
{
"recipients" : [
{
"name" : "Bill Gates",
"email" : "[email protected] /cdn-cgi/l/email-protection"
}
],
"orgs" : [
"Service D",
"Service E"
]
},
{
"recipients" : [
{
"name" : "Steve Jobs",
"email" : "[email protected] /cdn-cgi/l/email-protection"
}
],
"orgs" : [
"Service F",
"Service G"
]
}
]
}
使用 jq 我希望能够使用其中一个组织进行搜索,例如“服务 A”并仅返回收件人信息
我可以使用 jq 轻松搜索收件人,例如:
cat /path/to/file.json | jq -r '.groups[] | .recipients[] | select(.name | contains("Jobs"))' )
回来
{
"name": "Steve Jobs",
"email": "[email protected] /cdn-cgi/l/email-protection"
}
但是如果我尝试通过 orgs 数组进行搜索,则会出现错误:
cat /path/to/file.json | jq -r '.groups[] | select(.orgs | contains("Service A"))' )
jq: error (at <stdin>:46): array (["Service A...) and string ("Service A") cannot have their containment checked
是否可以用 jq 做我正在寻找的事情?