This is a step by step example of how to read a JSON file in a Python program,
Step 1:Make sure you import JSON module,
import json
Step 2:
Now lets read from an external JSON file,
json_file = open("sample-json-file.txt", "r")
Step 3:
Now we need to convert this json_file object to a String,
json_str = json_file.read()
Step 4:
Now lets convert this json_str to an JSON object,
json_obj = json.loads(json_str)
Step 5:
In this example, we will just iterate the array in the JSON file,
for element in json_obj:
print(element['name'])
print(element['age'])
print(element['city'])
print("-----------")
Complete Program:

#
# Code2care Python Programs
# How to read JSON File
#
import json
json_file = open("sample-json-file.txt", "r")
json_str = json_file.read()
json_obj = json.loads(json_str)
for element in json_obj:
print(element['name'])
print(element['age'])
print(element['city'])
print("-----------")
Output:
Mike 29 New York ----------- John 21 Chicago ----------- Sam 23 London ----------- Brian 19 Madrid ----------- Danny 27 New York -----------
- 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 Error CAA50021 - Number of retry attempts exceeds expectation - Microsoft 365 Apps - Microsoft
- Java 8 JDBC: Insert Timestamp Code Example - Java
- Install Visual Studio VS Code on Mac using Brew - MacOS
- Enable and reduce background noise in Microsoft Teams with AI-based noise suppression - Teams
- Using Java 8 Month Enum with Examples - Java
- Android Studio 1.3 beta now Available for Developers - Android-Studio
- How to Change Eclipse Default Web Browser - Eclipse
- Fix: Git Pull Error: unable to resolve reference refs/remotes/origin/master: reference broken - Git