Mantis Mantis - 5 months ago 77
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)

"id": "2",
"name": "Rob Croft",
"key": "0003837852"})

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download