Home > Mobile >  API JSON Response- replicating python pandas dataframe code into R
API JSON Response- replicating python pandas dataframe code into R

Time:01-16

I am trying to use this API: https://docs.oikolab.com/#2-1-request

And they offer extensive documentation. I have managed to get the data I want from the API but can't seem to figure out how to transform it into an R dataframe. The page offers an example and how to do it in Python to convert to a pandas dataframe, but I can't seem to get the same solution in R.

I have tried fromJSON() but I think due to the way the data is structured this is causing some issues.

Here is the example they give on the website, my question would be how to do the exact same thing but in R?

A repsonse from the API would look like this:

{'attributes': {
                'processing_time': 1.527,
                'n_parameter_months': 851, 
                'gfs_reference_time': '2021-05-17 12 UTC', 
                'next_gfs_update': 'in 2.0 hours (approx)', 
                 },
  'data': '{"columns": ["temperature (degC)","wind_speed (m/s)"],
                       "index":[1262304000,1262307600,... ],
                       "data":[[16.59,7.61],[16.44,7.79]...]
          }'
}

And the site says for Python and Pandas, convert to a dataframe like this:

import json
import pandas as pd

weather_data = json.loads(r.json()['data'])
df = pd.DataFrame(index=pd.to_datetime(weather_data['index'], 
                                       unit='s'),
                  data=weather_data['data'],
                  columns=weather_data['columns'])

So again, how would you replicate the outcome of this code in R?

Thank you!

CodePudding user response:

This is too long for a comment but from "https://oikolab.com/api-details#api=weather&operation=weather-data".

The request format looks like this: https://api.oikolab.com/weather/[?param][&start][&end][&location][&lat][&lon][&api-key][&resample_method][&freq][&format]

So an example request might look something like this:

url<- 'https://api.oikolab.com/weather/?temperature&start=2010-01-01&end=2010-12-31&lat=43.6529&lon=-79.3849&api-key=********'

To convert the JSON response to a data frame like this:

jsonlite::fromJSON(url)

If the response is a well formatted JSON, then the above should work, if not, it is very difficult to provide additional advice.

CodePudding user response:

As commented, if you review the structure of object using str(), you can identify the elements to extract. Below uses json snippet (corrected for double quotes):

library(jsonlite)

json_data = '{"attributes": {
  "processing_time": 1.527,
  "n_parameter_months": 851, 
  "gfs_reference_time": "2021-05-17 12 UTC", 
  "next_gfs_update": "in 2.0 hours (approx)" 
},
  "data": {"columns": ["temperature (degC)","wind_speed (m/s)"],
    "index":[1262304000, 1262307600],
    "data": [[16.59,7.61], [16.44,7.79]]
  }
}'

weather_data_obj <- fromJSON(txt = json_data)

str(weather_data_obj)
# List of 2
#  $ attributes:List of 4
#   ..$ processing_time   : num 1.53
#   ..$ n_parameter_months: int 851
#   ..$ gfs_reference_time: chr "2021-05-17 12 UTC"
#   ..$ next_gfs_update   : chr "in 2.0 hours (approx)"
#  $ data      :List of 3
#   ..$ columns: chr [1:2] "temperature (degC)" "wind_speed (m/s)"
#   ..$ index  : int [1:2] 1262304000 1262307600
#   ..$ data   : num [1:2, 1:2] 16.59 16.44 7.61 7.79

Then, build a data frame using the data.frame constructor similar to pandas:

weather_data_df <- data.frame(
  index = as.POSIXct(weather_data$data$index, origin = "1970-01-01"),
  `colnames<-`(weather_data$data$data, weather_data$data$columns),
  check.names = FALSE
)
  
weather_data_df
#                 index temperature (degC) wind_speed (m/s)
# 1 2009-12-31 18:00:00              16.59             7.61
# 2 2009-12-31 19:00:00              16.44             7.79
  •  Tags:  
  • Related