7

I have the following JSON file:

{"id":1140854908,"name":"'Amran"} 
{"id":1140852651,"name":"'Asir"} 
{"id":1140855190,"name":"'Eua"} 
{"id":1140851307,"name":"A Coruna"} 
{"id":1140854170,"name":"A`Ana"}

I used the package jsonlite but I get a parsing error

library(jsonlite) 
try <- fromJSON("states.txt",simplifyDataFrame = T)
# Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) :   
# parse error: trailing garbage
#           :1140854908,"name":"'Amran"} {"id":1140852651,"name":"'Asir"
#                      (right here) ------^
MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
user3006691
  • 435
  • 3
  • 7
  • 16

2 Answers2

6

Try changing your data file to below

[
{"id":1140854908,"name":"'Amran"} 
,{"id":1140852651,"name":"'Asir"} 
,{"id":1140855190,"name":"'Eua"} 
,{"id":1140851307,"name":"A Coruna"} 
,{"id":1140854170,"name":"A`Ana"}
]

The same code worked for me.. It is looking for an array..

vmachan
  • 1,672
  • 1
  • 10
  • 10
  • This solution has solved the same problem for me on two completely separate occasions, first with Twitter JSON data and secondly with Reddit JSON data. – timothyjgraham Apr 28 '17 at 07:55
  • 2
    While this answer was accepted as the correct one, I don't think that this answer will generally solve the problem. Under most circumstances, the user won't be able to manually change the json file. – Tea Tree Oct 01 '20 at 19:49
2

Your file is a newline delimited JSON (http://ndjson.org/). You can read it with jsonlite like this:

try <- stream_in(file("states.txt"))
Melkor.cz
  • 1,977
  • 17
  • 15