Arron
Arron

Reputation: 25

Parsing nested JSON data within keys

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 :)

Upvotes: 0

Views: 71

Answers (1)

Rakesh
Rakesh

Reputation: 82785

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"],))

Upvotes: 1

Related Questions