In this article, we will see how to read a JSON file in Python with examples,

The first thing you need to do is import the json module,
import json
Step 2:
Now let's create a JSON object that we want to read and store it in a variable,
json_str = """[
{
"name":"Mike",
"age":29,
"city":"New York"
},
{
"name":"John",
"age":21,
"city":"Chicago"
},
{
"name":"Sam",
"age":23,
"city":"London"
},
{
"name":"Brian",
"age":19,
"city":"Madrid"
},
{
"name":"Danny",
"age":27,
"city":"New York"
}
]"""
Step 3:
Now let's convert the JSON string to JSON Object
json_obj= json.loads(json_str)
Step 4:
Now let's read the content of JSON object, not that as we have a JSON Object of arrays, we can read it with its index starting from zero,
print(json_obj[0]["name"])
print(json_obj[0]["age"])
print(json_obj[0]["city"])
print("-----------------")
print(json_obj[1]["name"])
print(json_obj[1]["age"])
print(json_obj[1]["city"])
Full Example to Read JSON in Python:
import json
json_str = """[
{
"name":"Mike",
"age":29,
"city":"New York"
},
{
"name":"John",
"age":21,
"city":"Chicago"
},
{
"name":"Sam",
"age":23,
"city":"London"
},
{
"name":"Brian",
"age":19,
"city":"Madrid"
},
{
"name":"Danny",
"age":27,
"city":"New York"
}
]"""
json_obj = json.loads(json_str)
print(json_obj[0]["name"])
print(json_obj[0]["age"])
print(json_obj[0]["city"])
print("-----------------")
print(json_obj[1]["name"])
print(json_obj[1]["age"])
print(json_obj[1]["city"])
Output:
Mike
29
New York
-----------------
John
21
Chicago
If you are reading JSON where you do not know the size of it and want to iterate over it, you can use a for-each loop
for element in json_obj:
print(element['name'])
print(element['age'])
print(element['city'])
print("-----------")
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Android : Accidental Octal Lint Warning - Android
- Fix: Spring Boot + Caching: DefaultSerializer requires a Serializable payload - Java
- osascript wants to make changes while Android Studio Installation on Mac OS X - Mac-OS-X
- Java Code to check if Twitter app is installed on Android device - Android
- Remove items from JavaScript array - JavaScript
- [Fix] MySQL No database selected - ERROR 1046 (3D000) - MySQL
- How to show or hide columns in SharePoint Online List Library from - SharePoint
- Installing Native Chrome Browser App on M1 Mac Device - Chrome