Parse multiple JSON objects from file in Python

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.

CODE

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

{“name”: “ram”, “age”: 20, “city”: “Mumbai”}

{“name”:  “Hemant”, “age”: 33, “city”: “Chennai”}

{“name”: “Kalyan”, “age”: 25, “city”: “Banglore”}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top