[英]Selecting from nested array in JSON using jq gives empty result
我一直在為此絞盡腦汁,因為從表面上看,我使用的 jq 語句應該會產生預期的結果,但是使用 jqplay 之類的工具進行測試卻給了我一個空白的結果......
這是我正在使用的 JSON:
{
"DBInventory":
{
"InventoryID": "ABSCBD",
"Inventory":[
{
"Sys1":
{
"folder":[
{
"Scripts":
{
"files": [
"FileName1O",
"FileName2O"
]
}
},
{
"Schemas":
{
"files": [
"FileName1S",
"FileName2S"
]
}
},
{
"StoredProcedures":
{
"files": [
"FileName1SP",
"FileName2SP",
"FileName3SP"
]
}
},
{
"Tables":
{
"files": [
"FileName1T",
"FileName2T"
]
}
}
]
}
},
{
"Sys2":
{
"folder":[
{
"MaintScripts":
{
"files": [
"FileName1",
"FileName2"
]
}
},
{
"Schemas":
{
"files": [
"FileName1",
"FileName2"
]
}
},
{
"StoredProcedures":
{
"files": [
"FileName1",
"FileName2"
]
}
},
{
"Tables":
{
"files": [
"FileName1",
"FileName2"
]
}
},
{
"ExternalTables":
{
"files": [
"FileName1",
"FileName2"
]
}
},
{
"Scripts":
{
"files": [
"FileName1",
"FileName2"
]
}
},
{
"Views":
{
"files": [
"FileName1v",
"FileName2v"
]
}
}
]
}
}
]
}
}
我正在嘗試 select 和 object 只返回它下面的數組中的文件名(比如我只想要 'Sys1' 的 'Schemas' 中的文件)。 所需的 output 將是:
["Filename1S","Filename2S"]
從本節開始:
Inventory > Sys1 > folder > Schemas
我一直在調用的 jq 語句是:
.DBInventory.Inventory[0][] | select(any(.folder[]; .Key == "Schemas")) | .files[]
任何幫助將不勝感激。 我想我已經接近了,但是整個空白 output 讓我陷入了困境。
您在這里根本不需要select()
。 考慮:
jq '.DBInventory.Inventory[0][].folder[].Schemas?.files // empty'
JSON 數據不是完全規則的,因此您可以更精確地指定感興趣的路徑,或者在查詢中添加一些“救助”(例如// empty
或?
),例如:
.DBInventory.Inventory[].Sys1 // empty
| (.folder[].Schemas // empty).files
或者簡單地說:
try (.DBInventory.Inventory[].Sys1.folder[].Schemas.files) // empty
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.