Skip to content

Instantly share code, notes, and snippets.

@JakeRuss
Last active August 29, 2015 14:27
Show Gist options
  • Save JakeRuss/089b9003bef72b889224 to your computer and use it in GitHub Desktop.
Save JakeRuss/089b9003bef72b889224 to your computer and use it in GitHub Desktop.
Parse Claritin json string
library(jsonlite)
url <- "http://www.claritin.com/webservice/allergyforecast.php?zip=33956"
df <- fromJSON(url)
@hrbrmstr
Copy link

url <- "http://www.claritin.com/webservice/allergyforecast.php?zip=33956"
res <- readLines(url)
res <- gsub('^"|"%', "", gsub("\\\\", "", res))
str(jsonlite::fromJSON(res))

'data.frame': 1 obs. of  3 variables:
 $ pollenForecast :'data.frame':  1 obs. of  6 variables:
  ..$ zip      : chr "33956"
  ..$ city     : chr "SAINT JAMES CITY"
  ..$ state    : chr " FL"
  ..$ forecast :List of 1
  .. ..$ : num  3.5 3.4 5.4 4.5
  ..$ pp       : chr "Ragweed, Grass and Chenopods."
  ..$ timestamp: chr "8/11/2015 12:00:00 AM"
 $ weatherForecast:'data.frame':  1 obs. of  5 variables:
  ..$ date    : chr "2015-08-11T07:00:00-0400"
  ..$ city    : chr "SAINT JAMES CITY"
  ..$ state   : chr " FL"
  ..$ zip     : chr "33956"
  ..$ forecast:List of 1
  .. ..$ :'data.frame': 6 obs. of  9 variables:
  .. .. ..$ lowF       : chr  "79" "79" "77" "77" ...
  .. .. ..$ highF      : chr  "94" "89" "88" "88" ...
  .. .. ..$ iconNight  : chr  "29" "29" "29" "33" ...
  .. .. ..$ skyDay     : int  0 30 30 38 38 38
  .. .. ..$ skyNight   : int  29 29 29 33 47 47
  .. .. ..$ phraseNight: chr  "Partly Cloudy" "Partly Cloudy" "Partly Cloudy" "Mostly Clear" ...
  .. .. ..$ date       : chr  "2015-08-11T07:00:00-0400" "2015-08-12T07:00:00-0400" "2015-08-13T07:00:00-0400" "2015-08-14T07:00:00-0400" ...
  .. .. ..$ iconDay    : chr  NA "30" "30" "38" ...
  .. .. ..$ phraseDay  : chr  NA "Partly Cloudy" "Partly Cloudy" "AM Thunderstorms" ...
 $ result         : logi TRUE

@octonion
Copy link

Code:

library(httr)
library(jsonlite)

url <- "http://www.claritin.com/webservice/allergyforecast.php?zip=33956"

foo <- GET(url)
c <- content(foo, as="text")
c <- substring(c, 4)
json <- fromJSON(fromJSON(c))
str(json)

Output:

'data.frame':   1 obs. of  3 variables:
 $ pollenForecast :'data.frame':        1 obs. of  6 variables:
  ..$ zip      : chr "33956"
  ..$ city     : chr "SAINT JAMES CITY"
  ..$ state    : chr " FL"
  ..$ forecast :List of 1
  .. ..$ : num  3.5 3.4 5.4 4.5
  ..$ pp       : chr "Ragweed, Grass and Chenopods."
  ..$ timestamp: chr "8/11/2015 12:00:00 AM"
 $ weatherForecast:'data.frame':        1 obs. of  5 variables:
  ..$ date    : chr "2015-08-11T07:00:00-0400"
  ..$ city    : chr "SAINT JAMES CITY"
  ..$ state   : chr " FL"
  ..$ zip     : chr "33956"
  ..$ forecast:List of 1
  .. ..$ :'data.frame': 6 obs. of  9 variables:
  .. .. ..$ lowF       : chr  "79" "79" "77" "77" ...
  .. .. ..$ highF      : chr  "93" "89" "88" "88" ...
  .. .. ..$ iconNight  : chr  "29" "27" "29" "31" ...
  .. .. ..$ skyDay     : int  0 30 30 38 38 38
  .. .. ..$ skyNight   : int  29 27 29 31 47 47
  .. .. ..$ phraseNight: chr  "Partly Cloudy" "Mostly Cloudy" "Partly Cloudy" "Clear" ...
  .. .. ..$ date       : chr  "2015-08-11T07:00:00-0400" "2015-08-12T07:00:00-0400" "2015-08-13T07:00:00-0400" "2015-08-14T07:00:00-0400" ...
  .. .. ..$ iconDay    : chr  NA "30" "30" "38" ...
  .. .. ..$ phraseDay  : chr  NA "Partly Cloudy" "Partly Cloudy" "AM Thunderstorms" ...
 $ result         : logi TRUE

@hrbrmstr
Copy link

those extraneous 4 bytes show up alot in httr-json & xml responses. i need to poke at the httr/curl code more to see if i can narrow that down.

@JakeRuss
Copy link
Author

In case anyone else comes late to this thread. Jeroen Ooms explains that this is a UTF-8 byte order mark (BOM). The development version of jsonlite on github removes this mark automatically, supplies a warning, and then continues to parse the json. Will be version 0.9.17 once it's on CRAN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment