1

I want to parse a file that contains multiple JSON objects that are not enclosed in an array and are separated only by a line break. The file has the following schema:

{"id":1,"firstName":"John","lastName":"Doe"}
{"id":2,"firstName":"Bob","lastName":"Smith"}

As far as I know, the standard approach using json.load() doesn't work here, because the objects are not enclosed in an array. So is there an elegant way to parse such a file in Python without modifying it?

1 Answers1

1

If every json object is on its own line, you should be able to do something like

with open('/path/to/file') as data:
    objects = [json.loads(line) for line in data]
Holloway
  • 6,412
  • 1
  • 26
  • 33