Error when reading the first line in jsonl file in R

I have a file formatted in *.jsonl format, which I am trying to read in R and convert each of its objects to json. A reproducible data stored in the "filename.jsonl" is provided below.


I am using the following R code:


The problem that I am getting to is precisely related to reading the very first object of the data (i.e. content[1] in the code above). The error that I am getting is:

Error: lexical error: invalid char in json text.
(right here) ------^

Note that this error message does not occur when reading the second or third lines from the "filename.jsonl" data. I will appreciate if someone could please help me resolve this issue as I was unable to find a resolution online. Thanks in advance.

I just found this resource related to BOM signature that can get added in front of a Json file, which one can remove by formatting in Notepad++. Here is link to the resource: Weird character at start of json content type - Stack Overflow

I am not familiar with the .jsonl file type but it does look like .ndjson to me. I wonder if the ndjson package will help you here. Or, you can use jsonlite::stream_in to stick with that package


# option1
ndjson::stream_in(data, cls = "tbl")

# option2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.