Last Updated:

Working with json data in Python

While writing code, it often becomes necessary to write some data to the hard drive. In this we can help the json module, which is already sewn into the python. Let's see how it works.

json module


Javascript Object Notation, as the name implies, is written in Js and is a very simple format for exchanging data. In order to import it into the project, it is enough to specify "import json".

With this module, you can very easily and simply write Python objects to a file with a json extension, and then, when you run the program, this data can be read.


Writing something to a json file means serializing, and from a json file to a python is deserializing. Now let's look at everything in more detail.


Convert from json to python

There are two methods for serialization: dump(), which writes data to a file, dumps(), which writes data to a python string.

Python json serialization example

import json
dict = {
«name»: «Oleg»,
«age»: 25,
«city»: «Moskva»
serial = json.dumps(dict)
print(serial ) => {«name»: «Oleg», «age»: 25, «city»: «Moskva»}
Working with json data in Python

Here everything is quite simple: on the 2nd line of code, we create a dictionary in which we write the data that we want to save. On the 7th line, we write this data to the line Python. In order to write data to a file, you need to do the following:

with open(«output.json», «w») as for_write:
json.dump(dict , for_write)

Instead of the seventh and eighth lines of code, you need to write this code. Here we first open the file for writing, and then everything that is in the dictionary is written to the file.

It is worth paying attention to the fact that by default json can not work with the Cyrillic alphabet, and to "teach" it you need to specify the parameter "ensure_ascii=False"

first = {"lastname": "Pushkin"}
second = {"subject": "Mathematics"}
print(json.dumps(second, ensure_ascii=False))

Convert from python to json

The json module gives us the ability to read data from a file and write it to Python objects.

"researcher": {
"name": "Ford Prefect",
"species": "Betelgeusian",
relatives: [
"name": "Zaphod Beeblebrox",
"species": "Betelgeusian"
string1 = json.loads(for_json )

Encoding and decoding objects

Now let's look at serializing and deserializing complex objects. For example:

class Person:
def __init__(self, name, age): = name
self.age= 25

When serializing this object, we will get an error because json cannot handle custom objects by default. We need to help him with this by simplifying his object a little.

one = Person(«course1», 80)
two = Person(«course2», 90)
list_of_Person= [one, two ]

The easiest way is to represent the object in a dictionary.

Human = [{‘name’:, ‘age’: x.age} for x in list_of_Person]
with open(‘output.json’, ‘w’) as out:
json.dump(Human , out)

And in order to then read, you can use the following code:

with open(‘output.json’) as inp:
data = json.load(inp)
list_of_Person= [Test(x[‘name’], x[‘age’]) for x in data]

Example of working with json python


Now let's look at working with json in real life and at a more or less real task. First, let's import 2 libraries: json and requests. We need the second one in order to make API requests to the JSONPlaceholder service, which provides us with fake JSON data.

answer = requests.get("")
todos = json.loads(answer.text)
person_todo = {} # The number of completed tasks is related to the user id.
for item in todos:
if item["completed"]:
try: # number of users increases
person_todo[item["userId"]] += 1
except: # set count to 1 for each new user
person_todo[item "userId"]] = 1
top_person = sorted(person_todo.items(),
key=lambda x: x[1], reverse=True) # Sorted list of pairs
done_max = top_person[0][1]
persons = [] # list for those who completed the maximum number of tasks
for person, done in top_person:
if done < done_max:
max_persons = " and ".join(persons)

As a little warm-up and practice for you, I will not make a detailed description of the code. There are added all the necessary comments, according to which it is not difficult to guess what he is doing! Good luck!

Python Proficiency Test


I hope you have coped with the previous task, and now you are ready to take a small test that will help you better consolidate today's material.

  1. What is json and what is it for?
  2. What is data serialization and deserialization?
  3. Can json work with Cyrillic by default?
  4. What is the difference between "dump" and "dumps" and "loads"?
  5. Tell us about "Encoding Custom Types."

Now you're familiar with serializing and deserializing objects using json. It's time to put your knowledge into practice. So right now, implement a small application using the json library!

Good luck!