scharfmn
scharfmn

Reputation: 3661

pattern for saving newline-delimited json (aka linejson, jsonlines, .jsonl files) with python

With Python, I'm saving json documents onto separate lines like this:

from bson import json_util # pymongo

with open('test.json', 'ab') as f:
    for document in documents:
       f.write(json_util.dumps(document)+'\n')

and then reading like this:

with open('test.json') as f:
    for line in f:
        document = json_util.loads(line)

The ease and simplicity make me think that there must be a gotcha? Is this all there is to linejson, aka jsonlines?

Upvotes: 10

Views: 5534

Answers (1)

Cyphase
Cyphase

Reputation: 12022

Yes, that's all there is to it.

Upvotes: 4

Related Questions