Search code examples
rjsonrjson

forward slashes at the front of JSON data


This is a request for a JSON data file from Statistics Canada; it is the example URL for its new Web Data Service.

When I try to get the file via rjson, I get an error about an unexpected character

Error in fromJSON(file = census_url) : unexpected character 
'/'

This is the sample code

   #install.packages('rjson')
   library(rjson)
   #This is the sample URL from your web data help page. 
   census_url<-'https://www12.statcan.gc.ca/rest/census- 
   recensement/CPR2016.json? 
   lang=E&dguid=2016A000011124&topic=1&notes=0'
   #This returns an unexpected character
   fromJSON(file=census_url)

When I enter this URL in error image a JSON formatter, then I get errors pointing to two forward slashes.

When I look at the JSON documentation link, then it looks like forward slashes are used as commenting characters.

so, is this a problem at Statistics Canada's end? Or is there a workaround inside R to parse this data properly?


Solution

  • Before passing it to fromJSON, we may load the file and erase the first two characters:

    fromJSON(json_str = substring(readLines(census_url), 3))