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
extracted_objs = []
with open ( 'data.json' , 'r' ) as file :
for line in file :
json_obj = json.loads(line)
extracted_objs.append(json_obj)
print (extracted_objs)
|
Output:
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
custom_sep = ';'
with open ( 'jsf.txt' , 'r' ) as file :
file_content = file .read()
objects = file_content.split(custom_sep)
for obj_str in objects:
obj = json.loads(obj_str)
print (obj)
|
Output:
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 re
import json
pattern = r '{.*?}'
with open ( 'data.json' , 'r' ) as file :
file_cont = file .read()
json_objs = re.findall(pattern, file_cont)
for obj_string in json_objs:
obj = json.loads(obj_string)
print (obj)
|
Output:
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.
Share your thoughts in the comments
Please Login to comment...