首页 > 解决方案 > JSON import in data.table not working in R (RStudio)

问题描述

Good day guys,

I have this segment of the code that works fine by generating a table with the imported data:

library(jsonlite)
library(data.table)

response = fromJSON("https://www.cryptocompare.com/api/data/coinlist")
df = data.table::rbindlist(response$Data, fill=TRUE)
View(df)

But, when I try to pull historical prices for one or multiple coins by using this segment:

library(jsonlite)
library(data.table)

response = fromJSON("https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=10")
df = data.table::rbindlist(response$Data, fill=TRUE)
View(df)

I get this error message instead:

Error in data.table::rbindlist(response$Data, fill = TRUE) :    Item 1
of input is not a data.frame, data.table or list

I'm really puzzled because from the results that they both generate (by executing the web link in a browser, I get almost the same header data...

How can I fix this, please?

Thanks Yassine

[EDIT] After changing the code to:

library(jsonlite)
library(data.table)

response = fromJSON("https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=10")
df = data.table(response$Data, fill=TRUE)
View(df)

I get this (all the data in one cell): Result table screenshot

How can I convert the prices from being in 1 cell into rows, please?

Thanks Yassine

标签: rjsonrestapi

解决方案


response = fromJSON("https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=10")
# or
# response = fromJSON("https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&limit=10")


df = as.data.frame(response$Data)

# result
df

# Aggregated   TimeFrom     TimeTo  Data.time Data.high Data.low Data.open Data.volumefrom Data.volumeto Data.close Data.conversionType Data.conversionSymbol
# 1       FALSE 1584316800 1585180800 1584316800   5364.93  4454.22   5357.02       203116.86     996661883    5045.00              direct                      
# 2       FALSE 1584316800 1585180800 1584403200   5561.43  4950.90   5045.00       116314.49     618671972    5337.66              direct                      
# 3       FALSE 1584316800 1585180800 1584489600   5451.02  5024.71   5337.66       109210.96     576456806    5413.06              direct                      
# 4       FALSE 1584316800 1585180800 1584576000   6418.99  5268.33   5413.06       147007.71     868668217    6184.88              direct                      
# 5       FALSE 1584316800 1585180800 1584662400   6932.04  5691.69   6184.88       148232.44     947038098    6206.44              direct                      
# 6       FALSE 1584316800 1585180800 1584748800   6459.65  5876.79   6206.44        67262.29     415771089    6195.43              direct                      
# 7       FALSE 1584316800 1585180800 1584835200   6415.14  5750.68   6195.43        75451.24     457211402    5828.15              direct                      
# 8       FALSE 1584316800 1585180800 1584921600   6614.56  5703.64   5828.15       107692.99     667919370    6503.53              direct                      
# 9       FALSE 1584316800 1585180800 1585008000   6857.49  6406.64   6503.53       105151.69     700444488    6767.18              direct                      
# 10      FALSE 1584316800 1585180800 1585094400   6980.29  6483.00   6767.18        84721.04     566264216    6694.21              direct                      
# 11      FALSE 1584316800 1585180800 1585180800   6796.12  6581.48   6694.21        16824.06     112750770    6629.23              direct     


推荐阅读