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("-----------")
- Python List of Lists with Examples
- Fix: ERROR: Could not find a version that satisfies the requirement tkinter (from versions: none) ERROR: No matching distribution found for tkinter
- How to Convert Python Notebook .ipynb (JSON) to Executable .py Module
- ModuleNotFoundError: No module named qdarkstyle.colorsystem [Python]
- Python: Pandas Merge Indicator (Left, Right and Both) Example
- Fix: EOFError Exception in Python
- How to URL Decode a Query String in Python
- How to take user input from the console in a Python program
- Compare two lists in Python and return matches
- How to change the Python Default version
- Fix: KeyError: exception in Python
- How to get the Execution Time of A Python Program
- Fix: ModuleNotFoundError: No module named boto3 [Python]
- How to get unique values from a list in Python
- How to pip install Python Modules in VSCode
- Python: Fix - TypeError: NoneType object is not iterable
- How to delete a file using Python code example
- How to create a dictionary comprehension in Python
- Python: Get just the filename without extension using Path
- How to comment out a block of code in Python
- How to add two float numbers in Python
- Python: How to POST Json Data with HTTP Request
- Get MD5 Hash as Checksum of a String in Python
- Python - Convert float to String
- How to Parse XML String in Python
- Random Address Generator - Tools
- JavaScript: Convert an Image into Base64 String - JavaScript
- [fix] Java JDBC ConnectException: Connection refused - Java
- bash: ls command to see list files in current directory all subdirectories - Bash
- Android : Prevent App for rotation landscape or portrait - Android
- How to crop a screenshot on Mac/Macbook - MacOS
- Fix Error: Unknown command: --cask [Homebrew] - MacOS
- Java 8 Format Date and Time Examples - Java