0

The data looks like this

dc = {'statusID': 210,
 'employee': '{"result": [{"name": "Tom", "Salary": "$4530"}]}'}

I would like to access the employee name "Tom"

Here is what I tried

for key in dc.items():
    for values in key:
        print(values.result)

It gives me an error

AttributeError: 'str' object has no attribute 'result'
mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

You can use ast.literal_eval to parse the string:

from ast import literal_eval


dc = {
    "statusID": 210,
    "employee": '{"result": [{"name": "Tom", "Salary": "$4530"}]}',
}

emp = literal_eval(dc["employee"])
print(emp["result"][0]["name"])

Prints:

Tom
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91