You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言转换Binance API JSON为DataFrame时遇行数不匹配错误

Converting Binance API JSON Response to DataFrame in R (Rows Mismatch Error)

Hi everyone,

I'm a total forum newbie—only been working with R, JSON data, and APIs for 3 days now, so please forgive me if I don't explain things perfectly! I'm really excited to dive into learning all this though.

I’ve been trying to turn the JSON data from the Binance ETHBTC order book depth endpoint into a usable DataFrame in R, but I keep hitting an error. Here’s the simplified code I’m using:

library(jsonlite)
library(httr)
endpoint <- "https://api.binance.com/api/v1/depth?symbol=ETHBTC"
dt_text <- content(GET(endpoint), "text")
dt_json <- fromJSON(dt_text, flatten = TRUE)
df_GetDepth <- as.data.frame(dt_json)

When I run this, I get the following error:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 1, 0

After debugging, I can see the error pops up on the final line where I convert the JSON object to a data frame. I think the problem is that the first field in the JSON (lastUpdateId) only has 1 value, while the bids and asks fields contain nested arrays with multiple rows. Here’s a snippet of the JSON response to show the structure:

{
  "lastUpdateId": 1027024,
  "bids": [
    [
      "4.00000000", // PRICE
      "431.00000000", // QTY
      [] // Can be ignored
    ]
  ],
  "asks": [
    [
      "4.00000200",
      "12.00000000",
      []
    ]
  ]
}

I’d really appreciate any help figuring out how to fix this so I can get the data into a proper, usable DataFrame. Thanks so much in advance!

内容的提问来源于stack exchange,提问作者Adityendra Suman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:45:07