Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON parsing error, invalid character

Tags:

json

parsing

r

I am using fromJSON from the jsonlite package in [R] to call GetPlayerSummaries from the Steam API (https://developer.valvesoftware.com/wiki/Steam_Web_API) to get access to a user's data. For most calls it's working fine, but at some point I get an error:

    Error in feed_push_parser(readBin(con, raw(), n), reset = TRUE) : 
  lexical error: invalid bytes in UTF8 string.
          publicâ„¢ II: The Sith Lordsâ",               "gameid": "208580"          },
                     (right here) ------^ 

When I access the call in my browser I find a � on the spot where it is probably giving the error. I could Try-Catch but I'd really like to get this data. How to get around this?

like image 840
Raynor Avatar asked Oct 20 '22 00:10

Raynor


2 Answers

For my purpose, reading with readLines and then parsing it seemed to work

readlines <- readLines(link, warn = FALSE)
parse <- fromJSON(readlines)

I have no idea why and how this works, and may hence be not the most clean solution, but it seems to be robust for my purposes.

like image 179
Raynor Avatar answered Nov 02 '22 07:11

Raynor


You have to use use jsonlite's streaming function

 json_file <- stream_in(file("abc.json"))

It has been answered in Stack Overflow here:

Error parsing JSON file with the jsonlite package

and here:

Export JSON from Spark and input into R

like image 26
Amrita Avatar answered Nov 02 '22 07:11

Amrita