Extract Multiple JSON Objects from one File using Python
Last Updated :
26 Feb, 2024
Python is extremely useful for working with JSON( JavaScript Object Notation) data, which is a most used format for storing and exchanging information. However, it can become challenging when dealing with multiple JSON objects stored within a single file. In this article, we will see some techniques to easily extract multiple JSON Objects from a file.
Extracting multiple JSON Objects from One File
Below are some approaches of extracting multiple JSON Objects from one file in Python:
- Using json.load() with Line-by-Line reading
- Using custom separator
- Using Regular Expressions
Using json.load() with Line-by-Line reading
This approach involves reading file content line by line and parsing each line individually as JSON. json.load() is a built in function present in json module that takes a file object or a string which contains JSON data as input and returns a Python object. This approach is suitable when each line of file represents a separate JSON Object.
Python3
import json
# create a list to store extracted json objects
extracted_objs = []
# open the file in read mode
with open('data.json', 'r') as file:
# Iterate over each line
for line in file:
# Parse the JSON object from the current line
json_obj = json.loads(line)
extracted_objs.append(json_obj)
# print all extracted JSON Objects
print(extracted_objs)
Output:
.png)
Using Custom Separator
This approach uses custom separator which serve as a separator between individual objects in a file. Entire file content will be read into memory as a single string using file.read() function. The content string will split into substrings wherever the custom separator appears. This will divide the string into separate objects.
Python3
import json
# Define the custom separator
custom_sep = ';'
# Open the file
with open('jsf.txt', 'r') as file:
# Read the file content
file_content = file.read()
# Split the content using the custom separator
objects = file_content.split(custom_sep)
# Process each split part as a separate object
for obj_str in objects:
# Parse string into a Python object
obj = json.loads(obj_str)
print(obj)
Output:
.png)
Using Regular Expressions
In this approch we will make use of regular expressions for extracting JSON objects from a file. There is re module in python to work with regular expressions. we have defined the pattern to capture the JSON objects. Entire file content will be read and by using re.findall() method the defined pattern will be applied to the file content and it will return the list of strings of json objects found in a file, each string is passed to the json.loads() method to parse it to the python object.
Python3
# import required modules
import re
import json
# define re pattern to match Json Object
pattern = r'{.*?}'
# open a file
with open('data.json', 'r') as file:
file_cont = file.read()
# find all JSON Objectss from a file by passing re pattern
json_objs = re.findall(pattern, file_cont)
# parse each JSON object
for obj_string in json_objs:
obj = json.loads(obj_string)
print(obj)
Output:
.png)
Conclusion
In conclusion, we have explored three different approaches for extracting multiple JSON Objects from one file in Python. Each approach has its own advantages and use cases, depending on the structure of the data and specific requirements of the task at hand. By understanding and utilizing these techniques effectively, one can efficiently extract and process data.
Similar Reads
How to Merge Multiple JSON Files Using Python We are given multiple JSON files and our task is to merge those multiple JSON files into a single JSON file with the help of different approaches in Python. In this article, we will see how we can merge multiple JSON files in Python. JSON FilesBelow are the two JSON files that we will use in our art
4 min read
Iterate Through Nested Json Object using Python Working with nested JSON objects in Python can be a common task, especially when dealing with data from APIs or complex configurations. In this article, we'll explore some generally used methods to iterate through nested JSON objects using Python. Iterate Through Nested Json ObjectBelow, are the met
3 min read
How to Extract or Parse JSON from a String in Python Here, we are given a string and we have to parse JSON format from the string in Python using different approaches. In this article, we will see how we can parse JSON from a string in Python. Example: Input: json_string = '{"India": "Delhi", "Russia": "Moscow", "Japan": "Tokyo"}' Output: {'India': 'D
3 min read
Convert String to JSON Object - Python The goal is to convert a JSON string into a Python dictionary, allowing easy access and manipulation of the data. For example, a JSON string like {"name": "John", "age": 30, "city": "New York"} can be converted into a Python dictionary, {'name': 'John', 'age': 30, 'city': 'New York'}, which allows y
2 min read
Return Data in JSON Format Using FastAPI in Python FastAPI is a modern, fast, web framework for building APIs with Python 3.7+ based on standard Python type hints. It is designed to be easy to use and efficient, providing automatic generation of OpenAPI and JSON Schema documentation. In this article, we will see how to return data in JSON format usi
2 min read