Sometimes, we want to extract multiple JSON objects from one file with Python.
In this article, we’ll look at how to extract multiple JSON objects from one file with Python.
How to extract multiple JSON objects from one file with Python?
To extract multiple JSON objects from one file with Python, we put the JSON objects in a JSON array.
Then we call json.load
to parse the content of the JSON file.
For instance, we write
[
{
"ID": "12345",
"Timestamp": "20220101",
"Usefulness": "Yes",
"Code": [{ "event1": "A", "result": "1" }]
},
{
"ID": "1A35B",
"Timestamp": "20220102",
"Usefulness": "No",
"Code": [{ "event1": "B", "result": "1" }]
},
{
"ID": "AA356",
"Timestamp": "20220103",
"Usefulness": "No",
"Code": [{ "event1": "B", "result": "0" }]
}
]
in file.json.
Then we open the file and parse the file into a list of dicts with
import json
with open('file.json') as json_file:
data = json.load(json_file)
We call open
to open file.json.
And then we call json.load
to parse the json_file
content into a list of dicts with the JSON objects.
Conclusion
To extract multiple JSON objects from one file with Python, we put the JSON objects in a JSON array.
Then we call json.load
to parse the content of the JSON file.