Python Load Json From File

Jan 12, 2022

1 min read

Published in
Load JSON File in Python

Load JSON File in Python using load() function, but before that let’s have breif about the JSON.

Why JSON? What is a JSON File? What are the Advantages of Using a JSON File?

JSON is used to transfer data between different servers or from server to browser or browser to server. JSON is human readable format and it’s lightweight and easy to parse.

How to Read a JSON File in Python

Create a JSON file using sample data. Please use this data to create JSON file.

Sample of JSON data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
{
  "celebrities": {
    "celebrity": [
      {
        "firstName": "Tom",
        "lastName": "Cruise",
      },
      {
        "firstName": "Maria",
        "lastName": "Sharapova",
      },
      {
        "firstName": "Ariana ",
        "lastName": "Grande",
      }
    ]
  }
}

Here is an example of Python code which loads JSON file and print the data.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import json
 
# Opening JSON file
f = open('data.json')
 
# returns JSON object as
# a dictionary
data = json.load(f)
 
# Iterating through the json
# list
for i in data['emp_details']:
    print(i)
 
# Closing file
f.close()

json.load

If you are a python programmer, json.load is a handy function to load data from a json file. It returns JSON Object after reading and parsing the JSON File data.

How to use JSON Module in Python :

Sharing is caring!