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?

2
  • Is every json object guaranteed to be on a single line? Commented Apr 12, 2019 at 12:42
  • @Holloway Yes, that is guaranteed. Commented Apr 12, 2019 at 12:56

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments