Two very common causes:
- What you're providing to
fromJSON()
is not actually JSON, but HTML
- The JSON you're trying to parse is not valid JSON
If the error message contains '<!DOCTYPE html>
'
That means the data you're giving to fromJSON()
is in fact HTML (not JSON).
lexical error: invalid char in json text.
<!DOCTYPE html> <html> <head>
(right here) ------^
That can happen if you're trying to access an API and it's redirecting you to a login page, or a 'access forbidden' page, or similar, or if the server is down and instead of giving you JSON data, it gives you a message like "Server down for maintenance", fromJSON()
doesn't know how to handle that.
But to really figure out what's going on, write the output as a text file and explore it:
library(rvest)
library(tidyverse)
endpoint_url <- "http://localhost:3000/currencies" # Replace with your JSON url
html_nodes(read_html(endpoint_url), "body") %>% as.character %>% writeLines("file.txt")
readLines("file.txt")
In my case, I could spot:
You need to sign in or sign up before continuing.
Which told me the URL I was giving to fromJSON()
requires a password or API key.
Invalid JSON
The fromJSON()
function can only parse JSON if it's valid (formatted correctly).
JSON is sets of key-value pairs.
Valid JSON:
fromJSON('{"Name":"Jane", "Age":20}')
# $Name
# [1] "Jane"
# $Age
# [1] 20
Invalid JSON
fromJSON("sdfsdf")
Error: lexical error: invalid char in json text.
sdfsdf
(right here) ------^
Here are some tips for diagnosing what's wrong with your JSON:
- Firstly, you can paste your JSON into a JSON validator and it will give you a 'yes'/'no' answer as to whether your JSON is valid.
- To explore invalid JSON, open your JSON file with a text editor and search for the part of the JSON that the error message refers to (i.e. the text the
-----^
is pointing to).
- Do something about it. In my case I was happy to delete that part of the json data (it meant losing that data, but that was tolerable since it would make the rest of the JSON valid). Another option is to fix the invalid JSON, that could be trickier but possible.