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:
json.loads(file object)
Example: Suppose the JSON file looks like this:

We want to read the content of this file. Below is the implementation.
# Python program to read# json fileimportjson# Opening JSON filef=open('data.json',)# returns JSON object as# a dictionarydata=json.load(f)# Iterating through the json# listforiindata['emp_details']:print(i)# Closing filef.close()chevron_rightfilter_noneOutput:

- json.loads(): If you have a JSON string, you can parse it by using the
json.loads()method.json.loads()does not take the file path, but the file contents as a string, usingfileobject.read()withjson.loads()we can return the content of the file.Syntax:
json.loads(jsonstring) #for Json string json.loads(fileobject.read()) #for fileobject
Example: This exmaple shows reading from both string and JSON file. The file shown above is used.
# Python program to read# json fileimportjson# JSON stringa='{"name": "Bob", "languages": "English"}'# deserializes into dict# and returns dict.y=json.loads(a)print("JSON string = ", y)print()# JSON filef=open('data.json',"r")# Reading from filedata=json.loads(f.read())# Iterating through the json# listforiindata['emp_details']:print(i)# Closing filef.close()chevron_rightfilter_noneOutput:
Recommended Posts:
- How to read and write JSON file using Node.js ?
- Read, Write and Parse JSON using Python
- Append to JSON file using Python
- Saving Text, JSON, and CSV to a File in Python
- Reading and Writing JSON to a File in Python
- Convert Text file to JSON in Python
- How to read from a file in Python
- Python program to read file word by word
- Python program to read character by character from a file
- Python - Difference between json.dump() and json.dumps()
- Read a file line by line in Python
- Javascript | Program to read text File
- Python program to reverse the content of a file and store it in another file
- reStructuredText | .rst file to HTML file using Python for Documentations
- Python - XML to JSON
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

