【发布时间】:2019-01-17 21:50:24
【问题描述】:
有人知道如何实现吗?
我有一个数组,它有一个嵌套数组,比如 tagNames,我想选择 tagNames 包含“auto-test”的所有项目,而不是“auto-test2”。
{
"servers":[
{"id":1, "tagNames": ["auto-test", "xxx"]},
{"id":2, "tagNames": ["auto-test2", "xxxx"]}
]
}
到目前为止,我正在使用
echo '{"servers":[{"id":1,"tagNames":["auto-test","xxx"]},{"id":2,"tagNames":["auto-test2","xxxx"]}]}' |\
jq '[ .servers[] | select(.tagNames | contains(["auto-test"])) ]'
我有两条记录,但我只想要第一条。
[
{
"id": 1,
"tagNames": [
"auto-test",
"xxx"
]
},
{
"id": 2,
"tagNames": [
"auto-test2",
"xxxx"
]
}
]
所以我想要这个:
[
{
"id": 1,
"tagNames": [
"auto-test",
"xxx"
]
}
]
有什么想法吗?
【问题讨论】: