Hey everyone, welcome to Python Read JSON File Tutorial. In this tutorial we will learn what is JSON and how to read JSON files or string in python. You might have already heard about JSON. It is used in Android, Web Technologies, REST APIs and everywhere.
What Is JSON ?
- JSON stands for JavaScript Object Notation.
- It is a data interchange format in which you can transfer data from client to server and server to client.
- It is also used for inter application communication.
- JSON is extremely important in application development, specially when you are working in Rest APIs.
- It is based on a subset of Java Script.
- It is easy to read and write.
Why we use JSON?
In initial days of web development we used to work with HTML, html is like the de facto design pattern. Initially we used to create static pages but now, the world is changing and we are making dynamic pages. Dynamic pages simply means that the data which you get from the server will be dynamic or on your request you will get response.
- When we send request to the server, server response the data to the client. We can send data in text format from server to client but reading a data in a plain text is difficult for a program.
Let’s take an example
- Imagine you have 10 employees in your database, and upon a request you are giving the data of all the employees in a plain text format.
1 2 3 |
employee name is John Doe, employee address is Chicago |
- And assume we have the same thing with random strings for all the employees. Now a human can understand this thing by reading, but it is hard to understand for a programming language.
- So we need a well defined structure so that we can easily read the data in our program. And JSON is the structure that is being used widely.
- If you are building an application, with a front end that interacts with an API and communicates with a server. Then JSON is the standard format to use for sending and receiving data using your standard HTTP requests.
JSON Syntax Rule
- Uses key value pairs- {“name”:”Sam”}
- Uses double quotes around KEY.
- Must use the specified data types.
- File type is “.json”
- MIME type is “Application/json”
- You can learn more about JSON from this link.
Example
Let’s take an example how the json looks like –
1 2 3 4 5 6 7 8 9 10 11 |
{ "name":"Sam" "age":20 "address":{ "street":"Main street" "city":"Ranchi" } "salary":5000 } |
Python Read JSON File
Now, we will see how to read JSON files in python. It is not so much difficult and i am going to explain it in detail. So let’s get started
Creating JSON file
First of all we will create a json file. In this file for example i am writing the details of employees of a company. I have given the name employee.json to this file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
{ "employees": [ { "name": "Sam", "Dept": "HR", "salary": 5000 }, { "name": "Mayra", "Dept": "Sales", "salary": 6000 }, { "name": "Hisham", "Dept": "HR", "salary": 9000 }, { "name": "Arsh", "Dept": "Production", "salary": 10000 } ] } |
Here we have stored employee’s informations such as name, Dept and salary. Now lets move towards the coding part. In the above JSON first we have a JSON Object, inside the object we have a key named employees, this key employees holds an array where we have JSON objects containing employee information. Remember a JSON object is defined with Curly Braces {}, and a JSON array is defined with Square Braces [ ]. We can have object inside array and array inside object, so you can nest the your JSON data according to your need.
Creating a Python Script
Here I am using PyCharm for creating my Python projects, you can use any Code Editor. We just need a Python Script and a JSON file that we need to read.
Loading the json file
Now we have to load the json file into a python object. If we want to load json file we use json.load()
method. For this we have to do following things –
1 2 3 4 5 6 7 8 9 10 11 |
import json #open the file with open('employee.json') as f: data = json.load(f) #reading file for emp in data['employees']: print(emp) |
What We Did ?
In the above code we have done the following things –
- In first line we imported the json file.
- To load the json file first we need to open this file.
with open('employee.json') as f:
will open the desired file. Here the json file exists in the same directory that’s why we have not to specify the file path. But if your file resides in another directory you have to specify the proper file path. json.load(f)
is used to load the json file into python object.It takes an argument i.e., file name.- Now we have to read the data from json file.
- For reading data we have to start a loop that will fetch the data from the list.
- print(emp) method simply print the data of json file.
If we run the above code, we get following output.
This way we can see the data that are stored in json file. And you can see we are able to read the DATA and identify the different attributes of the data.
Accessing JSON file according to our choice
It is very easy to access the file as we want. For this we have to apply the following codes –
Accessing the name of employee
1 2 3 4 |
for emp in data['employees']: print(emp["name"]) |
In this way we can access only the name of employee from json file.Now the output will be like this-
Accessing the Department and salary of employee
1 2 3 4 |
for emp in data['employees']: print(emp["Dept"],emp["salary"]) |
This will read department and salary of each employee.The output will be –
So this is all about the Python Read JSON File Tutorial. If you have any doubt then comment. And please share this post with your friends if you think it is useful. Thank You 🙂