goutham_mi3
goutham_mi3

Reputation: 345

Error fetching the json from forbes website in R using jsonlite

Here is my code:

forbesList<-fromJSON('https://www.forbes.com/ajax/list/data?year=2018&uri=powerful-brands&type=organization')

Error Details:

Error in parse_con(txt, bigint_as_char) : 
  lexical error: invalid char in json text.
           (!doctype html) (html lang="en")
(right here) ------^

Please help me out, i tried in many ways to resolve this issue, but failed. Any help is much appreciated.

Upvotes: 1

Views: 2083

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522084

The first parameter of fromJSON needs to be actual JSON text, not a URL pointing to some JSON text. Try downloading the JSON content first, then make your call to fromJSON:

library(httr)

url <- "https://www.forbes.com/ajax/list/data?year=2018&uri=powerful-brands&type=organization"
req <- GET(url)
stop_for_status(req)
json <- content(req, "text")
forbesList <- fromJSON(json)

I have verified here that the JSON content from your URL parses correctly, so I don't think this should be a problem.

Upvotes: 1

Related Questions