Skip to main content
Taught by Tech Leads

Master pipelines, cloud & AI to become an operational Data Engineer.

DataScientist.fr
Image de Working with JSON data in Python - Interactive Tutorial
Python
JavaScript

Working with JSON data in Python - Interactive Tutorial

Photo de Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Published on 2 janvier 2025 · 8 min of reading

In a digital world where data is omnipresent, mastering its formatting and manipulation is essential. JSON, or JavaScript Object Notation, has become an essential language for structuring and exchanging information efficiently and readably. This article guides you through the fundamentals of JSON, exploring how to read and write this data with Python, and offers you the keys to skillfully interact with this format. Dive into the world of JSON and discover how to optimize your information exchanges in your technology projects.

Introduction to json

The JSON format, or JavaScript Object Notation, has become a key standard for data exchange between servers and web applications. Its adoption is due to its simplicity and readability, combined with its ability to represent complex data structures.

What is JSON?

JSON is a lightweight data interchange format inspired by JavaScript object syntax. It is essentially composed of key-value pairs, making it similar to dictionaries or objects in Python. Here is a simple example of JSON data:
json
In this example, we have an object representing a person with a name, an age, and a list of skills.

Why use JSON?

JSON is preferred for several reasons:
  • Readability: Unlike other formats like XML, JSON is easy to read due to its concise structure.
  • Interoperability: It is widely supported by most programming languages, facilitating data exchange between diverse systems.
  • Simplicity: Its syntax is straightforward, reducing errors when writing or reading data.
  • Compatibility: JSON can be easily converted into native objects in languages like JavaScript, Python, and many others.

Comparison with other formats

Here is a comparison between JSON and XML, another popular data format:
Criteria JSON XML
Readability Easily readable due to its clear and concise syntax Can be verbose and hard to read
File size Generally more compact Often larger due to tags
Data model Based on key-value pairs Based on a hierarchical structure
Native support Supported by many modern languages Often requires additional libraries

Use in web development

In web development, JSON is commonly used for:
  • RESTful APIs: APIs use JSON to send and receive data.
  • Configuration storage: JSON configuration files are easy to modify without requiring code reloads.
  • Data transfer: It is used to transfer data between a client and a server, often via AJAX.
Therefore, JSON is a powerful and flexible tool, essential for anyone working with modern applications requiring smooth communication between different technologies.

Writing json with python

When it comes to manipulating JSON data in Python, the standard library json provides robust tools for reading and writing JSON files. Here is how you can create and manipulate JSON data in your Python applications.

Writing JSON data

To convert Python data to JSON, we use the json.dump() function to write directly to a file, or json.dumps() to obtain a JSON string. Let's take a simple example:
python
In this example, json.dump() writes the Python object data to a file named data.json. The parameter indent=4 formats the JSON to make it more readable.

Converting to JSON string

If you need to convert data to JSON without writing it to a file, use json.dumps():
python
The json.dumps() method is useful when you need to manipulate or transmit JSON data as a string, for example, when working with APIs.

Handling complex data types

The json library in Python automatically handles standard data types like strings, numbers, lists, and dictionaries. However, for complex data types like custom objects or dates, you may need to provide a custom serialization function:
python
In this example, the serialize() function converts a datetime object to a string in ISO 8601 format.
Writing JSON data with Python is an essential task for any application that requires the exchange or storage of structured data. With Python's json library, you can easily integrate JSON serialization into your projects, ensuring smooth and efficient communication between your systems.

Reading json with python

Reading JSON data in Python is just as straightforward as writing it, thanks to the built-in json library. Whether you need to read JSON files or process JSON strings, Python offers convenient methods to facilitate these tasks.

Reading JSON files

To read JSON files, we use the json.load() function, which converts the JSON content of a file into a Python object. Here is an example:
python
In this example, the file data.json is opened in read mode, and json.load() is used to convert the JSON content into a Python dictionary, which we can then easily manipulate.

Reading JSON strings

If you have a JSON string, you can use json.loads() to convert it into a Python object. Here’s how it works:
python
Using json.loads() is particularly useful when dealing with JSON data from sources such as web APIs.

Error handling

When reading JSON, it's important to handle exceptions to avoid unexpected errors. Here is an example of error handling:
python
By using a try-except block, you can capture and handle JSON decoding errors, which improves the robustness of your application.
With these tools and techniques, reading JSON in Python becomes a simple and effective operation, allowing for smooth integration of JSON data into your projects.

Interacting with json

Interacting with JSON data in Python involves not only reading and writing files but also manipulating and exploiting this data to meet various application needs. Here are some techniques for effectively interacting with JSON.

Modifying JSON data

Once you have loaded JSON data into a Python object, you can manipulate it like any other Python dictionary or list. For example, let's add a skill to our JSON object:
python
This example shows how to load the data, modify it, and save it, enabling dynamic information management.

Searching within JSON data

To search for specific information in a JSON object, you can traverse the Python data structures obtained after loading. Here is an example of searching for a skill:
python
This method allows you to filter and search for information based on dynamic criteria.

Advanced serialization

In some cases, you may need to serialize complex data types. As we have seen previously, defining a custom serialization function helps convert these objects to JSON. Here’s how you might handle that for a complex list:
python
Using custom serialization functions allows you to precisely control how complex data is converted to JSON, ensuring maximum compatibility with your application needs.
With these techniques, interacting with JSON data in Python becomes not only possible but also practical and powerful, offering unmatched flexibility in processing information.

Conclusion

In conclusion, working with JSON in Python is an essential skill for any modern developer, given the prevalence of this format in web development and APIs. With Python's json library, we can not only read and write JSON files but also interact dynamically with the data they contain.

Summary of acquired skills

  • Writing and reading: You have learned to use json.dump() and json.load() to effectively write and read JSON files. This allows you to save and retrieve structured data with ease.
  • Data manipulation: Once JSON data is loaded into Python data structures, you are able to manipulate it as you would with any Python object. This includes adding, updating, and deleting information in JSON objects.
  • Custom serialization: To handle complex data types, you can define custom serialization functions, allowing you to convert unsupported objects into JSON-compatible formats.
  • Searching and filtering: With native Python functions, you can easily search and filter JSON data, allowing you to work with complex datasets effortlessly.

Practical applications

The skills acquired enable you to create applications capable of effectively communicating with other systems via standardized formats. Whether you are developing a web application that consumes a third-party API, creating configuration files for your programs, or storing user data, knowing how to manipulate JSON is undeniably useful.

Next steps

To deepen your skills, consider familiarizing yourself with third-party libraries like simplejson for additional features, or exploring web frameworks like Flask or Django, which use JSON to facilitate data exchanges. Working on practical projects will help solidify your knowledge and discover new ways to automate and manage complex data flows.
Ultimately, mastering JSON in Python opens the door to a multitude of possibilities in software development, enabling you to create robust and scalable solutions.

Want to go further?

This topic is part of our Become a Data Analyst course. Browse the full programme, or get it by email.

Share with

Photo de Romain DE LA SOUCHÈRE

Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.

» Learn More

Associated trainings

All our trainings
Image de la formation Become a Data Analyst
Become a Data Analyst
6 months
Intermediate
Guarantee