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.

{"lens_id":"049-046-612-457-696","jurisdiction":"US"}
{"lens_id":"011-002-622-427-296","jurisdiction":"US"}
{"lens_id":"0149-0416-6112-4527-6926","jurisdiction":"US"}

I am using the following R code:

library(jsonlite)
data="filename.jsonl"
content<-readLines(data)
fromJSON(content[1])

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.
{"lens_id":"049-046-612-4
(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

data="filename.jsonl"

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

# option2
library(jsonlite)
jsonlite::stream_in(file(data))

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.