In this tutorial, you will learn how to parse JSON file and extract data from file in Python? We will look at how we can use ‘json‘ module to read the JSON object and get the value from the JSON array?
The JSON stands for JavaScript Object Notation and it is a very popular lightweight data-interchanging format. We use the JSON to serialize the data to have a key pair value data. The JSON object looks like a dictionary in Python.
So, here we will see how you can read the JSON data in Python and extract the specific data from the file? We will use the ‘json‘ python module. You don’t need to install it using the pip because it’s an in-built module. You can just use it by importing it into the file.
Let’s get started to play with JSON data in Python.
Parse JSON Object in Python
In this example, we will parse the simple JSON object string that we will create in Python. So first thing you need to import the ‘json‘ module into the file.
import json
Then create a simple JSON object string in Python and assign it to a variable.
json_data = '{"python": 1, "php": 2, "c": 3, "vb": 4, "perl": 5}'
Now we will use the loads()
function from ‘json‘ module to load the JSON data from the variable. We store the JSON data as a string in Python with quotes notation.
Then we will use the dumps() function from the ‘json’ module to convert the Python string to a JSON object. Now it’s ready to print your JSON object in Python. See the following example code.
import json json_data = '{"python": 1, "php": 2, "c": 3, "vb": 4, "perl": 5}' json_load = (json.loads(json_data)) print(json.dumps(json_load, indent=4))
In the above code, we printed the JSON data and used the indent for indentation. You can also use the sort in print to sort the result. So when you run the above code you will get the output like below.
Output:
{ "python": 1, "php": 2, "c": 3, "vb": 4, "perl": 5 }
Now if you want to iterate this JSON data then just simply use the loop to prin each data one by one. I’m going to use for loop to print it.
json_load = (json.loads(json_data)) for x in json_load: print("%s: %d" % (x, json_load[x]))
Output:
python: 1 php: 2 c: 3 vb: 4 perl: 5
Hope you understand the basics of parsing the JSON object in Python and print the value from it. Let’s now work with the file to extract data using the ‘json‘ python module.
Parse JSON File in Python
In this example, we will learn how to extract data from a JSON file in Python. In the above example, we saw the parse simple JSON object, and in this example, we will do the same but first, we will create a JSON file with .json
extension.
Let’s create the json_data.json file with the following JSON object OR you can download it from here. You can also use my JSON formatter tool to minify and beautify JSON objects and download them.
{ "web": { "languages": [ { "id": "1", "name": "PHP", "website": "https://www.php.net/" }, { "id": "2", "name": "Python", "website": "https://www.python.org/" }, { "id": "3", "name": "Java", "website": "https://www.java.com/en/" } ] } }
To Load this JSON file in Python, we will first open this file in read mode using the open()
function.
import json with open('json_data.json', 'r') as json_file: json_load = json.load(json_file) print(json_load)
The above code will print your JSON data from the file. It will store the JSON as Python dictionaries, you can check the type of json_load variable using the type(json_load)
.
Now you can use it in Python and get the data from it that you want to use in the program. You can get the specific index data or you can loop through all data.
Let’s look at how to extract specific data from JSON file object in Python that we have printed above example.
Extract Specific Data from JSON File
As we have stored JSON in the json_data variable, we will use this variable to get the specific value from the JSON array.
I will use the key represent to index to get that value from the JSON object.
For example, if I want to print all ‘languages‘ from the ‘web‘ JSON object then I will use the following code.
print(json_load['web']['languages'])
Output:
[ {"id":"1","name":"PHP","website":"https://www.php.net/"}, {"id":"2","name":"Python","website":"https://www.python.org/"}, {"id":"3","name":"Java","website":"https://www.java.com/en/"} ]
If you want to get only first language use the following
print(json_load['web']['languages'][0])
Output:
{"id":"1","name":"PHP","website":"https://www.php.net/"}
Just use the index of the array to get any languages and I’m sure you know that the array index always starts from ‘0’.
JSON Array Loop Through Data
As you see in the above example, we fetched all languages and printed them as objects. Now if you want to loop the value and print each language one by one then we will use the for loop to do it.
See the following code.
import json with open('json_data.json', 'r') as json_file: json_load = json.load(json_file) data = json_load['web']['languages'] for x in data: print(x['id'], x['name'], x['website'])
Output:
1 PHP https://www.php.net/ 2 Python https://www.python.org/ 3 Java https://www.java.com/en/
Conclusion
So, in this article, we covered essential aspects of extracting data from JSON files in Python. By exploring topics such as parsing JSON objects, parsing entire JSON files, extracting specific data, and efficiently looping through JSON arrays
Hope you understand the tutorial on how to extract the data from a JSON file in Python and how to fetch specific data from a JSON array.
If you have questions please let me know in the comment section I would love to help you with that.
FAQs
To parse a JSON object in Python, you can use the json
module. Use the json.loads()
function to parse a JSON string into a Python dictionary.
Parsing a JSON object involves converting a JSON-formatted string into a Python dictionary, while parsing a JSON file typically entails reading a JSON file and converting its contents into a usable format, often a Python dictionary.
Use the parsed JSON data as a dictionary and navigate through its keys to extract specific data. Accessing nested keys allows you to pinpoint the desired information within the JSON structure.
Use a for
loop to iterate through the elements of a JSON array. Access each element within the loop and perform the necessary operations based on your requirements.
Yes, Python provides efficient ways to handle large JSON datasets. Utilize streaming techniques or read the JSON data in chunks to avoid loading the entire dataset into memory, ensuring optimal performance for processing large files.