Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON (using jsonlite) parsing error in R

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) ------^
like image 896
user3006691 Avatar asked Feb 08 '16 17:02

user3006691


People also ask

How do you handle JSON parsing error?

The most common way to handle JSON parse error is using try-catch block. If the JSON string is valid, it will return a JavaScript object. If the JSON string is invalid, it will throw a SyntaxError.

Why is JSON parse failing?

The "SyntaxError: JSON. parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.

Does JSON parse throw an error?

JSON. parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered.


1 Answers

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..

like image 66
vmachan Avatar answered Oct 19 '22 04:10

vmachan