![](/img/trans.png)
[英]Searching a list of objects with identical keys for specific key, value pairs with JQ
[英]Get specific key value pairs with jq
在下面的示例json中,我可以使用jq -M ' map(select(.charge == null)) '
過濾費用為null的記錄
[
{
"id": 1,
"name": "vehicleA",
"state": "available",
"charge": 100
},
{
"id": 2,
"name": "vehicleB",
"state": "available",
},
{
"id": 3,
"name": "vehicleB",
"state": "available",
"charge": 50
}
]
返回:
{
"id": 2,
"name": "vehicleB",
"state": "available",
}
一個人如何僅獲取id
和與過濾后的記錄的id
相關聯的值,以便將該步驟添加到上述查詢中將返回2
?
您的問題並不完全正確:
map(select(.charge == null))
的輸出不是示例中的對象,而是單個對象的數組 無論如何,您都可以像這樣從map
的結果中提取.id
:
jq -M 'map(select(.charge == null)) | .[].id' file.json
如果您想要不收費的商品ID數組,則可以使用以下過濾器:
.items | map(select(.charge == null) | .id)
如果希望枚舉值而不是將其收集到數組中,則更好:
.items[] | select(.charge == null) | .id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.