Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

json

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
like image 452
Mantis Avatar asked Jul 27 '15 11:07

Mantis


People also ask

What is json dumps () method?

The dump() method is used when the Python objects have to be stored in a file. The dumps() is used when the objects are required to be in string format and is used for parsing, printing, etc, . The dump() needs the json file name in which the output has to be stored as an argument.

What is the difference between json dump and json dumps?

json. dump() method used to write Python serialized object as JSON formatted data into a file. json. dumps() method is used to encodes any Python object into JSON formatted String.

What is the return type of json dumps?

dumps() takes in a json object and returns a string.

How do you write a JSON file in Python?

Method 2: Writing JSON to a file in Python using json.dump() Another way of writing JSON to a file is by using json. dump() method The JSON package has the “dump” function which directly writes the dictionary to a file in the form of JSON, without needing to convert it into an actual JSON object.

What is JSON dump in Python?

json.dump () in Python. The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json.

What is the difference between dumps () and dump () in Python?

The dump () needs the json file name in which the output has to be stored as an argument. The dumps () does not require any such file name to be passed.

What is the use of JSON module in Python?

json module in Python module provides a method called dump () which converts the Python objects into appropriate json objects. It is a slight variant of dumps () method. The dump () method is used when the Python objects have to be stored in a file.

What are the different parameters in JSON file?

Parameters: 1 indent : It improves the readability of the json file. ... 2 skipkeys: If the key is not of standard allowed types like int, float, string, None or bool, error will be generated while dumping them. ... 3 separator: This parameter takes up either one or two values. ... 4 sort_keys: This parameter takes Boolean value. ... More items...


1 Answers

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.

like image 158
Martijn Pieters Avatar answered Oct 23 '22 21:10

Martijn Pieters