Mantis Mantis - 22 days ago 9
JSON Question

Writing to JSON produces TypeError: dump() takes at least 2 arguments (1 given)

I am trying to load in a json file. Update it and write it back. Here is my attempt at it but I am getting an error:


TypeError: dump() takes at least 2 arguments (1 given)


with open('employees.json') as data_file:
employees = json.load(data_file)
data_file.close

employees['employees'].append({
"id": "2",
"name": "Rob Croft",
"key": "0003837852"})

with open('employees.json', 'w') as data_file:
json.dump(employees)
data_file.close

Answer Source

You forgot to pass in the file object:

json.dump(employees, data_file)

Since you are using the file object as a context manager with the with statement, you do not need to manually close the file. And using just data_file.close is entirely redundant since you are not even calling the file.close() method.