尝试使用 jq 搜索数组时,Array 和 String cannot have their containment checked error
Array and String cannot have their containment checked error when trying to search array using jq
我有一个 json 文件,大致如下所示:
{
"default": [
{
"name" : "Joe Bloggs",
"email" : "joe.bloggs@business.org"
}
],
"groups": [
{
"recipients" : [
{
"name" : "Jane Bloggs",
"email" : "jane.bloggs@business.org"
}
],
"orgs" : [
"Service A",
"Service B",
"Service C"
]
},
{
"recipients" : [
{
"name" : "Bill Gates",
"email" : "bill.gates@business.org"
}
],
"orgs" : [
"Service D",
"Service E"
]
},
{
"recipients" : [
{
"name" : "Steve Jobs",
"email" : "steve.jobs@me.com"
}
],
"orgs" : [
"Service F",
"Service G"
]
}
]
}
使用 jq 我希望能够使用其中一个组织进行搜索,例如 'Service A' 和 return 只有收件人信息
我可以使用 jq 轻松搜索收件人,例如:
cat /path/to/file.json | jq -r '.groups[] | .recipients[] | select(.name | contains("Jobs"))' )
到return
{
"name": "Steve Jobs",
"email": "steve.jobs@me.com"
}
但是,如果我尝试通过 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 做我正在寻找的东西?
而不是 contains
你需要 index
[docs] 来检查是否有一个值为 [=16 的索引=]:
.groups[] | select(.orgs | index("Service A"))
将输出:
{
"recipients": [
{
"name": "Jane Bloggs",
"email": "jane.bloggs@business.org"
}
],
"orgs": [
"Service A",
"Service B",
"Service C"
]
}
JqPlay demo
我们可以将其扩展为仅输出 recipients
,如下所示:
.groups[] | select(.orgs | index("Service A")) | .recipients | first
我们使用 first
来 select 来自 .recipients
数组的第一个对象。输出将是:
{
"name": "Jane Bloggs",
"email": "jane.bloggs@business.org"
}
JqPlay demo
我有一个 json 文件,大致如下所示:
{
"default": [
{
"name" : "Joe Bloggs",
"email" : "joe.bloggs@business.org"
}
],
"groups": [
{
"recipients" : [
{
"name" : "Jane Bloggs",
"email" : "jane.bloggs@business.org"
}
],
"orgs" : [
"Service A",
"Service B",
"Service C"
]
},
{
"recipients" : [
{
"name" : "Bill Gates",
"email" : "bill.gates@business.org"
}
],
"orgs" : [
"Service D",
"Service E"
]
},
{
"recipients" : [
{
"name" : "Steve Jobs",
"email" : "steve.jobs@me.com"
}
],
"orgs" : [
"Service F",
"Service G"
]
}
]
}
使用 jq 我希望能够使用其中一个组织进行搜索,例如 'Service A' 和 return 只有收件人信息
我可以使用 jq 轻松搜索收件人,例如:
cat /path/to/file.json | jq -r '.groups[] | .recipients[] | select(.name | contains("Jobs"))' )
到return
{
"name": "Steve Jobs",
"email": "steve.jobs@me.com"
}
但是,如果我尝试通过 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 做我正在寻找的东西?
而不是 contains
你需要 index
[docs] 来检查是否有一个值为 [=16 的索引=]:
.groups[] | select(.orgs | index("Service A"))
将输出:
{
"recipients": [
{
"name": "Jane Bloggs",
"email": "jane.bloggs@business.org"
}
],
"orgs": [
"Service A",
"Service B",
"Service C"
]
}
JqPlay demo
我们可以将其扩展为仅输出 recipients
,如下所示:
.groups[] | select(.orgs | index("Service A")) | .recipients | first
我们使用 first
来 select 来自 .recipients
数组的第一个对象。输出将是:
{
"name": "Jane Bloggs",
"email": "jane.bloggs@business.org"
}