0

I'm trying to parse nested JSON data. I'm trying to get the 'DisplayValue' for each key, my code is

json_obj = r.json()
for result in json_obj["Result"]:
    for employeeid in result["EmployeeId"]:
        cursor.execute("INSERT INTO employee_detail (EmployeeId) VALUES (%s)",
                       (result["DisplayValue"]))

However I get the response

KeyError 'DisplayValue'

JSON output

{
"IsError": "false",
"Status": 0,
"Message": "string",
"Result": [
    {
        "EmployeeId": {
        "DisplayValue": "PW180",
        "FieldHistory": []
        },

        "Title": {
        "DisplayValue": "Mr.",
        "FieldHistory": []
        },

Thanks :)

1
  • You don't need the second for loop just use result['EmployeeId']['DisplayValue'] Commented Sep 18, 2019 at 8:25

1 Answer 1

1

You have a nested dictionary.

Use:

json_obj = r.json()
for result in json_obj["Result"]:
    cursor.execute("INSERT INTO employee_detail (EmployeeId) VALUES (%s)",
                       (result['EmployeeId']["DisplayValue"],))
Sign up to request clarification or add additional context in comments.

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.