reading a json file in R: lexical error: invalid char in json text

user3083674 picture user3083674 · Dec 6, 2016 · Viewed 22.9k times · Source

Here is an example of the code I'm using:

library(jsonlite)
library(curl)

#url
url = "http://www.zillow.com/search/GetResults.htm?spt=homes&status=001000&lt=000000&ht=010000&pr=999999,10000001&mp=3779,37788&bd=0%2C&ba=0%2C&sf=,&lot=0%2C&yr=,1800&singlestory=0&hoa=0%2C&pho=0&pets=0&parking=0&laundry=0&income-restricted=0&pnd=0&red=0&zso=0&days=36m&ds=all&pmf=0&pf=0&sch=100111&zoom=6&rect=-91307373,29367814,-84759521,35554574&p=1&sort=globalrelevanceex&search=maplist&rid=4&rt=2&listright=true&isMapSearch=true&zoom=6"

#json
results_data_json = fromJSON(txt = url)

I used to be able to run similar code to this with no issue. Now I'm getting the following error:

Error in feed_push_parser(buf) : 
  lexical error: invalid char in json text.
                                   <html><head><title>Zillow: Real
                 (right here) ------^

Any ideas around this?

Answer

ADF picture ADF · Feb 22, 2018

This happened to me reading in a json from a file. The code worked one day, and then the next day I got this error. I was eventually able to circumvent the error although I do not understand why my solution works. First, I found a github post that suggested adding the readLines() function. Eg.

r_object <- fromJSON(readLines("file.json"))

When I did this the json loaded correctly but I got the following warning:

Warning message:
In readLines("file.json") : incomplete final line found on 'file.json'.

Then, for no particular reason, I tried adding an additional line to the bottom of the JSON. Just a blank line after the last curly bracket. And that fixed it. No idea why. If anyone knows why this worked, please drop a comment.