You are here because when you try to load and parse a JSON file with multiple JSON objects in Python, you received an error. json.decoder.JSONDecodeError: Extra data error
. The reason is that the json.load() method can only handle a single JSON object.
Further Reading:
- Solve Python JSON Exercise to practice Python JSON skills
The file is invalid if it contains more than one JSON object. When you try to load and parse a JSON file with multiple JSON objects, each line contains valid JSON, but as a whole, it is not a valid JSON as there is no top-level list or object definition. We can call JSON a valid JSON only when there is a top-level list or object definition.
For example, you wanted to read the following JSON file, filter some data, and store it into a new JSON file.
{"id": 1, "name": "Ault", "class": 8, "email": "ault@pynative.com"} {"id": 2, "name": "john", "class": 8, "email": "jhon@pynative.com"} {"id": 3, "name": "josh", "class": 8, "email": "josh@pynative.com"} {"id": 4, "name": "emma", "class": 8, "email": "emma@pynative.com"}
If your file contains a list of JSON objects, and you want to decode one object one-at-a-time, we can do it. To Load and parse a JSON file with multiple JSON objects we need to follow below steps:
- Create an empty list called
jsonList
- Read the file line by line because each line contains valid JSON. i.e., read one JSON object at a time.
- Convert each JSON object into Python
dict
using ajson.loads()
- Save this dictionary into a list called result jsonList.
Let’ see the example now.
import json
studentsList = []
print("Started Reading JSON file which contains multiple JSON document")
with open('students.txt') as f:
for jsonObj in f:
studentDict = json.loads(jsonObj)
studentsList.append(studentDict)
print("Printing each JSON Decoded Object")
for student in studentsList:
print(student["id"], student["name"], student["class"], student["email"])
Output:
Started Reading JSON file which contains multiple JSON document Printing each JSON Decoded Object 1 Ault 8 ault@pynative.com 2 john 8 jhon@pynative.com 3 josh 8 josh@pynative.com 4 emma 8 emma@pynative.com
So What Do You Think?
I want to hear from you. What do you think of this article? Or maybe I missed one of the ways to Parse multiple JSON objects from a file, Either way, let me know by leaving a comment below.
Also, try to solve the Python JSON Exercise to have a better understanding of Working with JSON Data in Python.