'Get element from python dictionary dump
I am actually working on a rest API functionality and I am able to get the response successfully. I converted the API text response to dictionary.
The converted dictionary is actually a bunch of nested dictionaries
The issue I am facing now is I want to access a particular element from the parent dictionary with its name.
I can currently fetch it using its index position but going forward the index position might change and hence I need to fetch and process the result using the name.
Sample JSON:
res=response.text
resJSON=json.loads(res)
print(resJSON)
Output:
{
"result": [
{
"serverGroupName": "Ent_Server",
"serverGroupDescription": "Ent Servers",
"serverGroupInstances": []
},
{
"serverGroupName": "db server",
"serverGroupDescription": "Database Servers",
"serverGroupInstances": [
"db1"
]
},
{
"serverGroupName": "default",
"serverGroupDescription": "The default server group.",
"serverGroupInstances": [
"def1"
]
},
{
"serverGroupName": "dvTest",
"serverGroupDescription": "test group",
"serverGroupInstances": [
"a",
"b",
"c",
"d"
]
},
{
"serverGroupName": "wls_Server",
"serverGroupDescription": "weblogic servers",
"serverGroupInstances": [
"wls1"
]
}
]
}
I am interested to retrieve the dictionary item where "serverGroupName": "dvTest" and the list of "serverGroupInstances": ["a","b","c","d"]
I can currently do that using the index position from the JSON dump print(resJSON['result'][3]) but need a more dynamic fetch based on name.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
