Back to articles list Articles
5 minutes read

How to Convert a String to JSON in Python

JSON stands for JavaScript Object Notation. Although its name indicates that it is associated with the JavaScript programming language, the JSON format is language-independent and frequently used in many different programming languages.

What Is a JSON File?

JSON files are commonly used in transferring data between computers. For instance, when downloading a file from an API, you often need to deal with JSON files. Here is a great article that explains downloading a file in Python from an API.

The following is an example of a JSON file:

	"employee": [
			"FirstName": "John",
			"LastName": "Doe",
			"Age": "29",
			"Profession": "Engineer"

			"FirstName": "Jane",
			"LastName": "Doe",
			"Age": "27",
			"Profession": "Doctor"

Files that store data in JSON format are called JSON files. These files are text-based, human-readable, and easy to process – all of which make them highly popular.

In this article, we will learn how to convert a string to JSON in Python and how to create JSON files from Python objects.

Working with JSON Files in Python

Python has a built-in library called json which provides simple and efficient methods for working with JSON files. Let’s go over some examples that demonstrate how to convert a string to JSON in Python and vice versa.

From JSON to Python Object: Deserializing

The following is a JSON string:

>>> example = '{"FirstName":"John", "LastName":"Doe","Age":29, "Profession":"Engineer"}'

We can use the loads() method of the json library to convert this string to a Python object:

>>> import json
>>> myobject = json.loads(example)

We have just converted JSON encoded data into a Python object. This process is called deserializing. The resulting Python object is a dictionary. A Python dictionary consists of key-value pairs and we can easily access its items using the keys. For example, if we want to access the FirstName in the myobject dictionary, we write:

>>> myobject["FirstName"]


If we have a JSON file and want to turn it into a Python object, we can use the load() method. Take a quick look at the “employee” JSON file at the beginning of the article. The following code block reads this file and saves it into a Python dictionary.

>>> with open("employee.json", "r") as read_file:
...     employee = json.load(read_file)
>>> print(employee)

{'employee': [{'FirstName': 'John', 'LastName': 'Doe', 'Age': '29', 'Profession': 'Engineer'}, {'FirstName': 'Jane', 'LastName': 'Doe', 'Age': '27', 'Profession': 'Doctor'}]}

Now employee is a Python dictionary object.

It is important to emphasize the difference between the json library’s load() and loads() methods. The load method is used for creating a Python object from a JSON file, whereas the loads() method converts a JSON string to a Python object.

From Python Object to JSON String: Serializing

Just like we can create a Python object from a JSON file, we can convert a Python object to a JSON string or file. This process is called serialization.

The dumps() method converts a Python dictionary to a JSON string. In the deserializing section, we created a dictionary called myobject. It can be converted back to a JSON string as follows:

>>> json.dumps(myobject)
'{"FirstName": "John", "LastName": "Doe", "Age": 29, "Profession": "Engineer"}'

The output is a string (notice the single quotes around the curly brackets), so we cannot access a specific key-value pair as we do with dictionaries.

This very simple string is not difficult to read. However, JSON strings can be much longer and have nested parts. For such cases, the dumps() method provides a more readable way of printing. We can pretty print this string by setting the optional indent parameter:

>>> print(json.dumps(myobject, indent=3))

   "FirstName": "John",
   "LastName": "Doe",
   "Age": 29,
   "Profession": "Engineer"

The dumps() method also has a parameter for sorting by key:

>>> print(json.dumps(myobject, indent=3, sort_keys=True))

   "Age": 29,
   "FirstName": "John",
   "LastName": "Doe",
   "Profession": "Engineer"

JSON files are often used for serialization (pickling), e.g. for when you want to maintain some data between the runs of your application. You can learn more about object serialization in this article.

Creating a JSON File with dump()

The dumps() method converts a Python object to a JSON formatted string. We can also create a JSON file from data stored in a Python dictionary. The method for performing this task is dump().

Let’s use the dump() method for creating a JSON file. We’ll use the employee dictionary we created in the previous section:

with open("new_employee.json", "w") as write_file:
    json.dump(employee, write_file, indent=4)

This creates a file called new_employee.json in your current working directory and opens it in write mode. Then, we use the dump() method to serialize a Python dictionary.

The dump() method takes two positional arguments. The first one is the object that stores the data to be serialized (here, a Python dictionary). The second one is the file to write the serialized data. The indent parameter is optional.

Printing in the Command Line

The tool() method of the json library allows for pretty-printing JSON files in the command line. Let’s try it on the new_employee.json file we created in the previous section.

The first step is to open a command-line interface. Then we need to change the directory to the location where the new_employee.json file is saved.

The following command will print the JSON file in a nice, clean format:

python -m json.tool new_employee.json

The following image shows how it looks in the Windows command prompt.

How to Convert a String to JSON in Python

Learn More About JSON and Python

We have covered how to read and write JSON files in Python. The built-in json library makes it easy to do both of these. One of the advantages of Python is the rich selection of built-in and third-party libraries that simplify most tasks.

If you are learning or plan to learn Python, our Learn Programming with Python track is a great way to start. It is designed for beginners and contains 5 interactive courses. The advantage of learning with an interactive course is that you get real, hands-on practice writing code; this is essential for learning a programming language. also offers an entire course dedicated to JSON Files in Python. The course is also interactive and contains 35 exercises. If you want to practice the concepts we’ve discussed in this article, this course is for you. Happy learning!