0

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?

4
  • 4
    Your json has duplicate keys and once you load this into python as a dict only the last (result) is kept. Commented Oct 11, 2018 at 8:34
  • Sorry, i have corrected it. Commented Oct 11, 2018 at 8:35
  • 3
    for data in s['result']: print(data['externalId']).. s being the json Commented Oct 11, 2018 at 8:37
  • Once the json string parsed using 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. Commented Oct 11, 2018 at 9:10

4 Answers 4

2

Then why not list comprehension:

print([i["externalId"] for i in json_op["result"]])

Or want formatted:

print('\n'.join([i["externalId"] for i in json_op["result"]]))
Sign up to request clarification or add additional context in comments.

Comments

1

Ok. First of all you have to import json lib at the beginning of your code.

Then, you are not iterating properly your dictionary object.

If you want to read your requestId key, just write

print(json_op['requestId'])

For iterating results:

json_op = json.loads(s)

print(json_op['requestId'])

for res in json_op['result']:
    print(res['seq'])
    print(res['GUID'])
    print(res['amount'])
    ...

Try and tell me something. I cannot run the code now.

Comments

1
for i in json_op["result"]:
    print (i["externalId"]) 

This works.

Comments

1
import json

s = """{
    "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"
        }]
}"""

json_op = json.loads(s)

for item in json_op['result']:
    print(item['externalId'])

Output:

1234567890
2345678901

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.