I have been struggling with json stuff. I have "unit_number" and "key" values and I want to find backing_uuid based on the unit_number and key values. How can I do that with python? I think like this jq query but I've never tried before. I don't know how can I call jq in python.
jq .guest_disk_facts[] | select(any(.attributes[]?; .unit_number=="2" and .key" == "2002")).backing_uuid and output: 9000da43-8471-57a6-8b18-4425a356b3cb
I have json file like this:
{
"changed": false,
"failed": false,
"guest_disk_facts": {
"0": {
"backing_uuid": "9000da43-8471-57a6-8b18-92381ab3c3f6",
"key": 2000,
"unit_number": 0
},
"1": {
"backing_uuid": "9000da43-8471-57a6-8b18-2788c2398ba7",
"key": 2001,
"unit_number": 1
},
"2": {
"backing_uuid": "9000da43-8471-57a6-8b18-4425a356b3cb",
"key": 2002,
"unit_number": 2
}
}
}
for thing in dic['guest_disk_facts']:
if thing['key'] == key and thing['unit_number'] == unit_number:
print(thing['backing_uuid'])
break
Since.unit_number and.key are already numbers, the jq query would be:
.guest_disk_facts[]
| select(.unit_number==2 and .key == 2002).backing_uuid
As for using python with jq, see eg Is there a way to execute jq from python
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.