我一直在努力处理json内容。我有“ unit_number”和“ key”值,我想根据unit_number和key值找到backing_uuid。如何使用python做到这一点?我认为像这样的jq查询,但我从未尝试过。我不知道如何在python中调用jq。
jq .guest_disk_facts[] | select(any(.attributes[]?; .unit_number=="2" and .key" == "2002")).backing_uuid and output: 9000da43-8471-57a6-8b18-4425a356b3cb
我有这样的json文件:
{
"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
}
}
}
答案 0 :(得分:0)
for thing in dic['guest_disk_facts']:
if thing['key'] == key and thing['unit_number'] == unit_number:
print(thing['backing_uuid'])
break
答案 1 :(得分:0)
由于.unit_number和.key已经是数字,因此jq查询为:
.guest_disk_facts[]
| select(.unit_number==2 and .key == 2002).backing_uuid
关于将python与jq一起使用,请参见例如Is there a way to execute jq from python