【发布时间】:2019-01-23 19:23:40
【问题描述】:
我想使用 contains 从数组中选择特定项目,并使用 JQ 获取第一个项目。
JQ:
.amazon.items[] | select(.name | contains ("shoes"))
JSON:
{
"amazon": {
"activeitem": 2,
"items": [
{
"id": 1,
"name": "harry potter",
"state": "sold"
},
{
"id": 2,
"name": "adidas shoes",
"state": "in inventory"
},
{
"id": 3,
"name": "watch",
"state": "returned"
},{
"id": 4,
"name": "adidas shoes",
"state": "in inventory"
}
]
}
}
预期结果:
{
"activeitem": 2,
"item": {
"id": 2,
"name": "adidas shoes",
"state": "in inventory"
}
}
实际:
尝试了各种选项,例如但没有得到预期的响应。
.amazon.items[] | select(.name | contains ("shoes")).amazon.items | select(.[].name | contains ("shoes")) | .[0]
另外,当我尝试组合 activeitem 和 item 时,我得到了类似的结果,这也是错误的。
{
"activeitem": 2,
"item": {
"id": 2,
"name": "adidas shoes",
"state": "in inventory"
}
},
{
"activeitem": 2,
"item": {
"id": 2,
"name": "adidas shoes",
"state": "in inventory"
}
}
【问题讨论】: