0

I readin a JSON file and converted it to a dataframe with output below

'data.frame':   8 obs. of  46 variables:
 $ dayOfWeek                    : Factor w/ 7 levels "Friday","Monday",..: 5 
1 3 4 2 6 7 5
 $ expirationTimeUtc            : int  1532035962 1532035962 1532035962 
1532035962 1532035962 1532035962 1532035962 1532035962
$ moonPhase                    : Factor w/ 2 levels "First Quarter",..: 1 2 2 
2 2 2 2 2
$ moonPhaseCode                : Factor w/ 2 levels "FQ","WXG": 1 2 2 2 2 2 2 
2
$ moonPhaseDay                 : int  7 8 9 10 11 12 13 13
$ moonriseTimeLocal            : Factor w/ 8 levels "2018-07-19T13:46:10- 
0400",..: 1 2 3 4 5 6 7 8

What is the best way of unpacking this into a dataframe?

r<-GET("https://api.weather.com/v3/wx/forecast/daily/7day? 
geocode=33.74,-84.39&format=json&units=e&language=en-US&apiKey=yourkey")

weather <- content(r, "text")

wData <- fromJSON(weather, simplifyDataFrame = TRUE)

weatherDF <- data.frame(wData)
head(weatherDF)
Alli
  • 59
  • 3
  • 9
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What does the JSON actually look like? What does is the desired output? It's unclear what kind of "unpacking" you want to do. It looks like you already have a data.frame there. – MrFlick Jul 19 '18 at 21:18
  • 1
    What are you using to parse the json, `jsonlite::fromJSON`? jsonlite::stream_in`? `rjson::...`? My experience with these json-parsing libraries is that they do not give factors, so I'm at a slight loss here, so +1 to @MrFlick's comment on a reproducible question. – r2evans Jul 19 '18 at 21:21
  • I have modified my question to include the code – Alli Jul 20 '18 at 00:02
  • @Alli Just a suggestion but if you include your API key in your question we'll be able to run your code and see what JSON data is extracted from the API, and therefore have the best chance to suggest some way of making it into a dataframe. With my currently knowledge of your data (which is gained only from what you provide in the question), I would suggest you consider whether you need all that data in a df. Often some JSON response could include a LOT of into, much of it unnecessary. So my advice would be figure out the particular info you're after, look for it, then manipulate it into a df – stevec Jul 20 '18 at 02:58

0 Answers0