I've been racking my brains over this because on its face, the jq statement I'm using should produce the desired result, but using a tool like jqplay to test gives me a blank result....
Here is the JSON I'm using:
{
"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"
]
}
}
]
}
}
]
}
}
I'm trying to select an object only return the filenames in the array under it like so (say I wanted just the files in 'Schemas' for 'Sys1'). The desired output would be:
["Filename1S","Filename2S"]
from this section:
Inventory > Sys1 > folder > Schemas
The jq statement I've been invoking is:
.DBInventory.Inventory[0][] | select(any(.folder[]; .Key == "Schemas")) | .files[]
Any help would be greatly appreciated. I think I'm close, but the whole blank output has thrown me for a loop.
You don't need a select()
at all here. Consider:
jq '.DBInventory.Inventory[0][].folder[].Schemas?.files // empty'
The JSON data is not completely regular, so you could specify the path of interest more precisely, or sprinkle some "bailouts" (such as // empty
or ?
) into the query, eg:
.DBInventory.Inventory[].Sys1 // empty
| (.folder[].Schemas // empty).files
or simply:
try (.DBInventory.Inventory[].Sys1.folder[].Schemas.files) // empty
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.