[英]extract information from a json file with jq
我試圖獲取 Id:如果植物的名稱是 corn-1 並且價格是 20,則打印 Id
{
"Id": "Category-1",
"Plants":
[
{
"Name": "corn-1",
"Price": "20"
},
{
"Name": "corn-2",
"Price": "10"
},
{
"Name": "corn-3",
"Price": "5"
},
]
}
cat plants.json | jq -C 'select(.Plants[].Name=="corn-1" and .Price=="20").Id
但沒有打印出來。 我應該得到 Category-1。 有什么想法嗎?
你幾乎在那里:
jq -C 'select(.Plants[] | (.Name == "corn-1" and .Price == "20")).Id'
問題是您在頂部.Price
的上下文中使用了 .Price,所以它永遠不會匹配。
如果一個 Plants 中有多個匹配項,則 Id 將被打印多次。 要只獲得一次,您可以使用
jq -C 'select(.Plants | any(.Name == "corn-1" and .Price == "20")).Id'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.