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("-----------")
- tkinter - Hello World! Program
- How to install Python Specific version (3.8, 3.9 or 3.10) using Brew
- How to install SpaCy (NLP Library) on Mac
- Python matplotlib segmentation fault: 11 macOS Big Sur
- How to uninstall pip Python packages
- 3 Ways to find if element is present in a List in Python
- How to Convert Python String to DateTime Object
- Python f-strings Formatted String Literals Syntax and Examples
- Where does brew install python in macOS
- Take input argument from command line in Python Programming
- Advanced print() Function Tutorial and Techniques for Python Developers
- How to add borders to tkinter label text
- Whats new in Python 3.10 Pre-release
- Float built-in function in Python
- List of All 35 Reserved Keywords in Python Programming Language 3.11
- How to check if Key Exists in Python Dictionary?
- Read a file line by line in Python Program
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- What is the Max and Minimum Value of int type in Python?
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- Fix: TypeError: can only concatenate str (not int) to str in Python
- How to take user input from the console in a Python program
- [Fix] TypeError: str object is not callable in Python
- 3 Python program to add two numbers
- How to delete a file using Python code example
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics - Java
- How to Enable spellcheck Notepad++ - NotepadPlusPlus
- Best Free Gif screen capture app now available for M1 Chip Mac - LICECap - MacOS
- How to view the desktop when using macOS Stage Manager? - MacOS
- How to uninstall/remove or disable Microsoft Teams - Teams
- Command to unzip or extract tar.gz file on Linux or macOS? - Linux
- How to add duration to Android Toast makeText method - Android
- AlertDialog with single button example : Android - Android