Open In App

Extract Multiple JSON Objects from one File using Python

Last Updated : 26 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Screenshot-(359)

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:

Screenshot-(361)

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:

Screenshot-(361)

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads