How to parse json file python
How to parse json file python
Read JSON file using 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. To use this feature, we import the json package in Python script. The text in JSON is done through quoted-string which contains the value in key-value mapping within < >.
Reading From JSON
It’s pretty easy to load a JSON object in Python. Python has a built-in package called json, which can be used to work with JSON data. It’s done by using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.
Deserialization of JSON
The Deserialization of JSON means the conversion of JSON objects into their respective Python objects. The load()/loads() method is used for it. If you have used JSON data from another program or obtained as a string format of JSON, then it can easily be deserialized with load()/loads(), which is usually used to load from string, otherwise, the root object is in list or dict. See the following table given below.
JSON OBJECT | PYTHON OBJECT |
---|---|
object | dict |
array | list |
string | str |
null | None |
number (int) | int |
number (real) | float |
true | True |
false | False |
json.load(): json.load() accepts file object, parses the JSON data, populates a Python dictionary with the data and returns it back to you.
Syntax:
Example: Suppose the JSON file looks like this:
We want to read the content of this file. Below is the implementation.
Python Parse JSON – How to Read a JSON File
JSON (JavaScript Object Notation) is a popular way to structure data. It’s used to exchange information between a web application and the server. But how do you read a JSON file in Python?
In this article, I will show you how to use the json.loads() and json.load() methods to parse and read JSON files and strings.
JSON syntax
Before we get into parsing and reading a JSON file, we first need to understand the basic syntax. The JSON syntax looks like a JavaScript object literal with key-value pairs.
This is an example of JSON data for freeCodeCamp:
How to parse a JSON string in Python
Python has a built in module that allows you to work with JSON data. At the top of your file, you will need to import the json module.
If you need to parse a JSON string that returns a dictionary, then you can use the json.loads() method.
How to parse and read a JSON file in Python
In this example, we have a JSON file called fcc.json which holds the same data from earlier concerning the courses offered by freeCodeCamp.
If we want to read that file, we first need to use Python’s built in open() function with the mode of read. We are using the with keyword to make sure that the file is properly closed.
If the file cannot be opened, then we will receive an OSError. This is an example of a «FileNotFoundError» if I misspell the fcc.json file name.
The final step would be to print the results.
This is what the entire code would look like:
How to Pretty Print JSON data in Python
If we examine the printed data, then we should see that the JSON data prints all on one line.
In this example, we are going to have an indent of 4 spaces and print the data in an easier to read format.
Conclusion
JSON (JavaScript Object Notation) is a popular way to structure data and is used to exchange information between a web application and the server.
If you need to parse a JSON string that returns a dictionary, then you can use the json.loads() method.
If you need to parse a JSON file that returns a dictionary, then you can use the json.load() method.
I am a musician and a programmer.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
Reading & Parsing JSON Data With Python: Tutorial
JSON is a common standard used by websites and APIs, and even natively supported by modern databases such as PostgreSQL. In this article, we’ll present a tutorial on how to handle JSON data with Python. Now, let’s start with the definition of JSON.
What is JSON?
JSON, or JavaScript Object Notation, is a format that uses text to store data objects. In other words, it is data structures for representing objects as text. Even though it’s derived from JavaScript, it has become a de facto standard of transferring objects.
This format is supported by most popular programming languages, including Python. Most commonly, JSON is used to transfer data objects by APIs. Here is an example of a JSON string:
In this example, JSON data looks like a Python dictionary. Just like dictionaries, JSON contains data in key-value pairs. However, the JSON data can also be a string, a number, a boolean, or a list.
Before JSON became popular, XML had been the common choice to represent data objects in a text format. Here is an example of the same information in the XML format:
As evident here, JSON is lightweight. This is one of the primary reasons why JSON is so popular. If you want to read more about the JSON standard, head over to the official JSON website.
JSON in Python
The json module provides the functionality to write custom encoders and decoders, and there is no separate installation needed. You can find the official documentation for the Python JSON module here.
In the remainder of this tutorial, we will explore this package. We’re going to convert JSON to dictionary and list and the other way round. We’ll also explore how to handle custom classes.
Converting JSON string to Python object
JSON data is frequently stored in strings. This is a common scenario when working with APIs. The JSON data would be stored in string variables before it can be parsed. As a result, the most common task related to JSON is to parse the JSON string into the Python dictionary. The JSON module can take care of this task easily.
Note that the first method looks like a plural form, but it is not. The letter ‘S’ stands for ‘string’.
Let’s start with a simple example. The instance of JSON data is as follows:
JSON data can be stored as JSON string before it is parsed. Even though we can use Python’s triple quotes convention to store multi-line strings, we can remove the line breaks for readability.
The output of this snippet will confirm that this is indeed a JSON string:
We can call the json.loads() method and provide this string as a parameter.
The output of this snippet will confirm that the JSON data, which was a string, is now a Python dictionary.
This dictionary can be accessed as usual:
It is important to note here that the json.loads() method will not always return a dictionary. The data type that is returned will depend on the input string. For example, this JSON string will return a list, not a dictionary.
The following table shows JSON objects and the Python data types after conversion. For more details, see Python docs.
JSON | Python |
object | dict |
array | list |
string | str |
number (integer) | int |
number (real) | float |
true | True |
false | False |
null | None |
Now, let’s move on to the next topic on parsing a JSON object to a Python object.
Converting JSON file to Python object
Reading JSON files to parse JSON data into Python data is very similar to how we parse the JSON data stored in strings. Apart from JSON, Python’s native open() function will also be required.
The load() method takes up a file object and returns the JSON data parsed into a Python object.
To get the file object from a file path, Python’s open() function can be used.
Save the following JSON data as a new file and name it united_states.json :
Enter this Python script in a new file:
Running this Python file prints the following:
In this example, the open function returns a file handle, which is supplied to the load method.
This variable data contains the JSON as a Python dictionary. This means that the dictionary keys can be checked as follows:
Using this information, the value of name can be printed as follows:
In the previous two sections, we examined how JSON can be converted to Python objects. Now, it’s time to explore how to convert Python objects to JSON.
Converting Python object to JSON string
Here is a simple example. Save this code in a new file as a Python script:
When this file is run with Python, the following output is printed:
The Python object is now a JSON object. This simple example demonstrates how easy it is to parse a Python object to a JSON object. Note that the Python object was a dictionary. That’s the reason it was converted into a JSON object type. Lists can be converted to JSON as well. Here is the Python script and its output:
Refer to the conversion table below for details. As you can see, only the dictionary is converted to json object type. For the official documentation, see this link.
Python | JSON |
dict | object |
list, tuple | array |
str | string |
int, float, int | number |
True | true |
False | false |
None | null |
Writing Python object to a JSON file
Here is a simple demonstration. This will open the file in writing mode and write the data in JSON format. Save this Python script in a file and run it.
When this code is executed using Python, countries_exported.json file is created (or overwritten) and the contents are the JSON.
However, you will notice that the entire JSON is in one line. To make it more readable, we can pass one more parameter to the dump() function as follows:
This time when you run the code, it will be nicely formatted with indentation of 4 spaces:
Note that this indent parameter is also available for JSON dumps() method. The only difference between the signatures of JSON dump() and JSON dumps() is that dump() needs a file object.
Converting custom Python objects to JSON objects
Let’s examine the signature of dump() method:
Save the following code as a Python script and run it:
To convert the objects to JSON, we need to write a new class that extends JSONEncoder. In this class, the method default() should be implemented. This method will have the custom code to return the JSON.
Here is the example Encoder for the Country class. This class will help converting a Python object to a JSON object:
This code is simply returning a dictionary, after confirming that the supplied object is an instance of Country class, or calling the parent to handle the rest of the cases.
This class can now be supplied to the json.dump() as well as json.dumps() methods.
Creating Python class objects from JSON objects
So far, we have discussed how json.load() and json.loads() methods can create a dictionary, list, and more. What if we want to read a JSON object and create a custom class object?
In this section, we will create a custom JSON Decoder that will help us create custom objects. This custom decoder will allow us to use the json.load() and json.loads() methods, which will return a custom class object.
We will work with the same Country class that we used in the previous section. Using a custom encoder, we were able to write code like this:
If we try to parse this JSON file using the json.load() method, we will get a dictionary:
Apart from writing this method, we would also need to call the __init__ method of the base class and set the value of the parameter object_hook to this method name. For simplicity, we can use the same name.
Finally, we can call the json.load() method and set the cls parameter to CountryDecoder class.
That’s it! We now have a custom object created directly from JSON.
Loading vs dumping
Here is a quick table to help you remember these functions:
File | String | |
Read | load() | loads() |
Write | dump() | dumps() |
Conclusion
In this tutorial, we explored reading and writing JSON data using Python. Knowing how to work with JSON data is essential, especially when working with websites. JSON is used to transfer and store data everywhere, including APIs, web scrapers, and modern databases like PostgreSQL. You can click here to find the complete code used in this article for your convenience.
Understanding JSON is crucial if you are working on a web scraping project that involves dynamic websites. Head over to our blog post for a practical example of JSON being useful for pages with infinite scroll.
JSON in Python: How To Read, Write, and Parse
JSON, short for JavaScript Object Notation, is an open standard. Although its name doesn’t imply so, it is a language-independent data format. With Python’s JSON library, we can read, write, and parse JSON, in order to both store and exchange data using this versatile data format. It’s a prevalent data format because it is easy to read and write for humans too, although not as easy as YAML!
Working with JSON in Python is super easy! Python has two data types that, together, form the perfect tool for working with JSON in Python: dictionaries and lists.
Table of contents
Importing the JSON library
Python ships with a powerful and elegant JSON library to help you decode and encode JSON. You can import the module with:
This library is part of Python, so you don’t need to install it with the Pip package manager.
How to parse JSON in Python
Here’s an example of json.loads in action:
If the interactive example above doesn’t work (it’s still in beta), here’s a more static example instead:
The output might look like a string, but it’s actually a dictionary that you can use in your code as explained on our page about Python dictionaries. You can check for yourself:
Encoding JSON with json.dumps
Encoding JSON data with Python’s json.dumps is just as easy as decoding. Use json.dumps (short for ‘dump to string’) to convert a Python object consisting of dictionaries, lists, and other native types into a string:
Here’s the same example, in case the above interactive example doesn’t work in your browser:
This is the same document, converted back to a string! If you want to make your JSON document more readable for humans, use the indent option. It will nicely format the JSON, using space characters:
Pretty printing JSON on the command line
Python’s JSON module can also be used from the command line. It will both validate and pretty-print your JSON:
You may also be interested in using the jq-tool for this though!
How to read a JSON file in python
How to write JSON to a file in Python
The json.dump function is used to write data to a JSON file. You’ll need to open the file in write mode first:
Frequently Asked Questions
Simply use the methods described above. The json.dump and json.dumps functions accept both dictionaries and lists
Similar to arrays, so use json.dump or json.dumps on the dictionary.
By default: no. The library outputs ASCII and will convert characters that are not part of ASCII. If you want to output Unicode, set ensure_ascii to False. Example: json.dumps(data, ensure_ascii=False)
Keep learning
Related posts you might like
About Erik van Baaren
Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He’s been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.
Python Read JSON File – How to Load JSON from a File and Parse Dumps
Welcome! If you want to learn how to work with JSON files in Python, then this article is for you.
You will learn:
Are you ready? Let’s begin! ✨
🔹 Introduction: What is JSON?
The JSON format was originally inspired by the syntax of JavaScript (a programming language used for web development). But since then it has become a language-independent data format and most of the programming languages that we use today can generate and read JSON.
Importance and Use Cases of JSON
JSON is basically a format used to store or represent data. Its common use cases include web development and configuration files.
🔸 JSON Structure and Format
Now that you know what the JSON format is used for, let’s see its basic structure with an example that represents the data of a pizza order:
These are the main characteristics of the JSON format:
💡 Tip: The values that require quotes have to be surrounded by double quotes.
💡 Tip: We typically format JSON with different levels of indentation to make the data easier to read. In this article, you will learn how to add the indentation automatically with Python.
JSON Data Types: Keys and Values
JSON files have specific rules that determine which data types are valid for keys and values.
Style Guide
🔹 JSON vs. Python Dictionaries
JSON and Dictionaries might look very similar at first (visually), but they are quite different. Let’s see how they are «connected» and how they complement each other to make Python a powerful tool to work with JSON files.
JSON is a file format used to represent and store data whereas a Python Dictionary is the actual data structure (object) that is kept in memory while a Python program runs.
How JSON and Python Dictionaries Work Together
When we work with JSON files in Python, we can’t just read them and use the data in our program directly. This is because the entire file would be represented as a single string and we would not be able to access the key-value pairs individually.
We use the key-value pairs of the JSON file to create a Python dictionary that we can use in our program to read the data, use it, and modify it (if needed).
This is the main connection between JSON and Python Dictionaries. JSON is the string representation of the data and dictionaries are the actual data structures in memory that are created when the program runs.
Great. Now that you know more about JSON, let’s start diving into the practical aspects of how you can work with JSON in Python.
🔸 The JSON Module
We will use this module in the coming examples.
How to Import the JSON Module
To use json in our program, we just need to write an import statement at the top of the file.
With this line, you will have access to the functions defined in the module. We will use several of them in the examples.
💡 Tip: If you write this import statement, you will need to use this syntax to call a function defined in the json module:
🔹 Python and JSON Strings
To illustrate how some of the most important functions of the json module work, we will use a multi-line string with JSON format.
JSON String
Particularly, we will use this string in the examples. It is just a regular multi-line Python string that follows the JSON format.
💡 Tip: The Python Style Guide recommends using double quote characters for triple-quoted strings.
JSON String to Python Dictionary
We will use the string with JSON format to create a Python dictionary that we can access, work with, and modify.
To do this, we will use the loads() function of the json module, passing the string as the argument.
This is the basic syntax:
Here is the code:
Let’s focus on this line:
Awesome! If we print this dictionary, we see this output:
The dictionary has been populated with the data of the JSON string. Each key-value pair was added successfully.
Now let’s see what happens when we try to access the values of the key-value pairs with the same syntax that we would use to access the values of a regular Python dictionary:
Exactly what we expected. Each key can be used to access its corresponding value.
💡 Tip: We can use this dictionary just like any other Python dictionary. For example, we can call dictionary methods, add, update, and remove key-value pairs, and more. We can even use it in a for loop.
JSON to Python: Type Conversion
When you use loads() to create a Python dictionary from a JSON string, you will notice that some values will be converted into their corresponding Python values and data types.
This table presented in the Python Documentation for the json module summarizes the correspondence from JSON data types and values to Python data types and values:
Table presented in the official documentation of the json module
💡 Tip: The same conversion table applies when we work with JSON files.
Python Dictionary to JSON String
Now you know how to create a Python dictionary from a string with JSON format.
But sometimes we might need to do exactly the opposite, creating a string with JSON format from an object (for example, a dictionary) to print it, display it, store it, or work with it as a string.
To do that, we can use the dumps function of the json module, passing the object as argument:
💡 Tip: This function will return a string.
This is an example where we convert the Python dictionary client into a string with JSON format and store it in a variable:
Let’s focus on this line:
If we print this string, we see this output:
If we check the data type of this variable, we see:
So the return value of this function was definitely a string.
Python to JSON: Type Conversion
A process of type conversion occurs as well when we convert a dictionary into a JSON string. This table from the Python Documentation illustrates the corresponding values:
Table from the official documentation of the json module.
How to Print JSON With Indentation
If we use the dumps function and we print the string that we got in the previous example, we see:
But this is not very readable, right?
We can improve the readability of the JSON string by adding indentation.
To do this automatically, we just need to pass a second argument to specify the number of spaces that we want to use to indent the JSON string:
💡 Tip: the second argument has to be a non-negative integer (number of spaces) or a string. If indent is a string (such as «\t» ), that string is used to indent each level (source).
Now, if we call dumps with this second argument:
The result of printing client_JSON is:
That’s great, right? Now our string is nicely formatted. This will be very helpful when we start working with files to store the data in a human-readable format.
How to Sort the Keys
You can also sort the keys in alphabetical order if you need to. To do this, you just need to write the name of the parameter sort_keys and pass the value True :
💡 Tip: The value of sort_keys is False by default if you don’t pass a value.
Returns this string with the keys sorted in alphabetical order:
How to Sort Alphabetically and Indent (at the same time)
To generate a JSON string that is sorted alphabetically and indented, you just need to pass the two arguments:
In this case, the output is:
💡 Tip: You can pass these arguments in any order (relative to each other), but the object has to be the first argument in the list.
Great. Now you know how to work with JSON strings, so let’s see how you can work with JSON files in your Python programs.
🔸 JSON and Files
Typically, JSON is used to store data in files, so Python gives us the tools we need to read these types of file in our program, work with their data, and write new data.
How to Read a JSON File in Python
Let’s say that we created an orders.json file with this data that represents two orders in a pizza shop:
Please take a moment to analyze the structure of this JSON file.
Here are some quick tips:
If we want to read this file in Python, we just need to use a with statement:
💡 Tip: In the syntax above, we can assign any name to file (green box). This is a variable that we can use within the with statement to refer to the file object.
The key line of code in this syntax is:
Once we have the content of the JSON file stored in the data variable as a dictionary, we can use it to do basically anything we want.
Examples
For example, if we write:
The output is 2 because the value of the main key «orders» is a list with two elements.
We can also use the keys to access their corresponding values. This is what we typically do when we work with JSON files.
For example, to access the toppings of the first order, we would write:
You can see this «path» graphically in the diagram:
If we print this value, the output is:
Exactly what we expected. You just need to «dive deeper» into the structure of the dictionary by using the necessary keys and indices. You can use the original JSON file/string as a visual reference. This way, you can access, modify, or delete any value.
💡 Tip: Remember that we are working with the new dictionary. The changes made to this dictionary will not affect the JSON file. To update the content of the file, we need to write to the file.
How to Write to a JSON File
Let’s see how you can write to a JSON file.
The first line of the with statement is very similar. The only change is that you need to open the file in ‘w’ (write) mode to be able to modify the file.
💡 Tip: If the file doesn’t exist already in the current working directory (folder), it will be created automatically. By using the ‘w’ mode, we will be replacing the entire content of the file if it already exists.
There are two alternative ways to write to a JSON file in the body of the with statement:
Let’s see them in detail.
First Approach: dump
This is a function that takes two arguments:
Let’s say that the pizza shop wants to remove the clients’ data from the JSON file and create a new JSON file called orders_new.json with this new version.
We can do this with this code:
This was the original version of the data in the orders.json file. Notice that the «client» key-value pair exists.
This is the new version in the orders_new.json file:
If you analyze this carefully, you will see that the «clients» key-value pair was removed from all the orders.
However, there is something missing in this file, right?
Please take a moment to think about this. What could it be?
Indentation, of course!
Now the content of the file looks like this:
What a difference! This is exactly what we would expect a JSON file to look like.
🔹 load() vs. loads()
This table summarizes the key differences between these two functions:
💡 Tip: Think of loads() as «load string» and that will help you remember which function is used for which purpose.
🔸 dump() vs. dumps()
Here we have a table that summarizes the key differences between these two functions:
💡 Tip: Think of dumps() as a «dump string» and that will help you remember which function is used for which purpose.
🔹 Important Terminology in JSON
Finally, there are two important terms that you need to know to work with JSON:
🔸 In Summary
I really hope you liked my article and found it helpful. Now you know how to work with JSON in Python. Follow me on Twitter @EstefaniaCassN and check out my online courses.