I am having trouble with parsing a specific key from a json string in a table. Below is my code to read a csv file and extract "employee_id" from the json column in each row:
with open('data.csv') as csvFile:
csv_reader = csv.reader(csvFile, delimiter=',')
for row in csv_reader:
event_data = row[4] # the output
data = json.loads(event_data)
print(data['employee_id'])
Here is a sample event_data output:
"{\"py/object\": \"employee_information.event_types.EmployeeCreated\", \"employee_id\": \"98765\", \"employee_first_name\": \"Jonathan\", \"employee_last_name\": \"Smith\", \"application_id\": \"1234\", \"address\": \"1234 street\"}"
But I get an error saying
Traceback (most recent call last):
File "/Users/someuser/Documents/python_test/main.py", line 12, in <module>
data = json.loads(event_data)
File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
When I pass the actual json string into json.loads(), it works fine but passing event_data doesn't for some reason.