16

I can't figure out how to create JSONL using Python3.

test = [{'a': 'b'}, {'a': 'b'}, {'a': 'b'}]

with open("data.json", 'w') as f:
    for item in test:
        json.dump(item, f)

with open("data.json") as f:
    for line in f:
        // only 1 line here! 
        print(line)

// prints
{"a": "b"}{"a": "b"}{"a": "b"}

I've tried using indent option to dump but it appears to make no different and the separators option don't seem to be a great usecase. Not sure what I'm missing here?

2
  • Do you want each item in test as a new line in the output file? Commented Jul 17, 2019 at 9:03
  • @Rakesh yes that's exactly what i want. Commented Jul 17, 2019 at 9:08

2 Answers 2

26

Use .write with newline \n

Ex:

import json
test = [{'a': 'b'}, {'a': 'b'}, {'a': 'b'}]

with open("data.json", 'w') as f:
    for item in test:
        f.write(json.dumps(item) + "\n")

with open("data.json") as f:
    for line in f:
        print(line)
Sign up to request clarification or add additional context in comments.

2 Comments

You're right, was overthinking it - that does seem to be valid JSONL. Thanks
You can also use jsonlines, or ndjson :)
1

You can also use Pandas to do it.

import pandas as pd

df_test = pd.DataFrame([{'a': 'b'}, {'a': 'b'}, {'a': 'b'}])

with open("data.jsonl", "w") as f:
    f.write(df_test.to_json(orient="records", lines=True, force_ascii=False))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.