I have the following JSON input
{
"requestId": "453sdafwa234",
"result": [
{
"seq": 0,
"GUID": "081119bd-63a8-42ca-85de-5b4761234955",
"amount": 1234,
"externalId": "1234567890",
"Status": "OK"
},
{
"seq": 1,
"GUID": "011119bd-42ca-63a8-85de-5b47111a8955",
"amount": 5678,
"externalId": "2345678901",
"Status": "OK"
}]
}
I want to loop through all result and then output the externalId key value.
I tried
json_op = json.loads(json_string)
for op in json_op:
for r in op["result"]:
print r["externalId"]
which did not work.
I also tried
json_op = json.loads(json_string)
for op in json_op:
r.get["result"].get["externalId"]
But this also didnt work. What is the right way?
jsonhas duplicate keys and once you load this into python as adictonly the last (result) is kept.json.loads(), what you have is a plain python dict (where on of the keys maps to a python list of python dicts). The fact it's been built from a json string is totally irrelevant, it's just plain python dicts and lists, both types being fairly well documented.