2

Consider having the following code and a jsonl file,
there is a specific reason I don't read file with jsonlines.open() api, so please take this as a fact.

Reference for jsonlines package: https://jsonlines.readthedocs.io/en/latest/#jsonlines.Reader

import jsonlines

with open('example.jsonl', 'r') as jsonl_f:
    content = jsonl_f.read()

with jsonlines.Reader(content) as reader:
    lst = [obj for obj in reader]

example.jsonl content:

{"hello": "world"}
{"covid": "19"}

Error I get on lst= line:

 lst = [obj for obj in reader]
  File "../lib/python3.7/site-packages/jsonlines/jsonlines.py", line 204, in iter
    skip_empty=skip_empty)
  File "../lib/python3.7/site-packages/jsonlines/jsonlines.py", line 164, in read
    six.raise_from(exc, orig_exc)
  File "<string>", line 3, in raise_from
jsonlines.jsonlines.InvalidLineError: line contains invalid json: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) (line 1)
JavaSa
  • 5,813
  • 16
  • 71
  • 121

1 Answers1

5
import jsonlines

with jsonlines.open('example.jsonl', 'r') as jsonl_f:
     lst = [obj for obj in jsonl_f]

The jsonl_f is the reader and can be used directly. It contains the lines in the json file.

NargesooTv
  • 837
  • 9
  • 15