过滤并打印Json输出



我正在寻找有关打印json数据与过滤器定义的帮助。

下面是我的实际输出,但我只想打印我需要的字段,如期望输出中定义的那样。

{
"response": {
"@status": "success",
"result": {
"enabled": "yes",
"group": {
"mode": "Active-Active",
"local-info": {
"url-compat": "Match",
"app-version": "xxxxxx",
"gpclient-version": "Not Installed",
"build-rel": "xxxxx",
"ha2-port": "dedicated-ha2",
"av-version": "0",
"ha2-keep-alive": "split-datapath",
"url-version": "0000.00.00.000",
"ha1-backup-ipaddr": "xxxxx",
"mgmt-hb": "configured",
"platform-model": "xxxx",
"av-compat": "Match",
"vpnclient-compat": "Match",
"ha1-ipaddr": "xxxxx",
"ha1-backup-macaddr": "xxxxxx",
"vpnclient-version": "Not Installed",
"ha2-macaddr": "xxxxxx",
"monitor-fail-holdup": "0",
"priority": "100",
"preempt-hold": "1",
"state": "active-primary",
"version": "1",
}
}
}
}
}


我在我的代码中使用了什么:

json_data = json.dumps(output)

print (json_data[0][mode])

期望输出:

{

"mode"Active-Active",

"state"active-primary",

}

输出我得到:{

您可以尝试这样过滤输出:

import json
output = {
"response": {
"@status": "success",
"result": {
"enabled": "yes",
"group": {
"mode": "Active-Active",
"local-info": {
"url-compat": "Match",
"app-version": "xxxxxx",
"gpclient-version": "Not Installed",
"build-rel": "xxxxx",
"ha2-port": "dedicated-ha2",
"av-version": "0",
"ha2-keep-alive": "split-datapath",
"url-version": "0000.00.00.000",
"ha1-backup-ipaddr": "xxxxx",
"mgmt-hb": "configured",
"platform-model": "xxxx",
"av-compat": "Match",
"vpnclient-compat": "Match",
"ha1-ipaddr": "xxxxx",
"ha1-backup-macaddr": "xxxxxx",
"vpnclient-version": "Not Installed",
"ha2-macaddr": "xxxxxx",
"monitor-fail-holdup": "0",
"priority": "100",
"preempt-hold": "1",
"state": "active-primary",
"version": "1",
}
}
}
}
}

json_data = json.dumps(output, indent=4)

print("Mode: "+ output["response"]["result"]["group"]["mode"])
print("State: "+ output["response"]["result"]["group"]["local-info"]["state"])

最新更新