我在将JSON转换为可用数据框时遇到问题。当源JSON具有多个级别时,就会出现问题,并且使用fromJSON的结果最终是较大数据帧中的数据帧列。该数据帧列在其内部也具有列表和数据帧。我想展平整个文件以检索一个数据框并从中选择必要的列。
我可以使用Shopify API为例:
"line_items": [
{
"fulfillable_quantity": 1,
"fulfillment_service": "amazon",
"fulfillment_status": "fulfilled",
"grams": 500,
"id": 669751112,
"price": "199.99",
"product_id": 7513594,
"quantity": 1,
"requires_shipping": true,
"sku": "IPOD-342-N",
"title": "IPod Nano",
"variant_id": 4264112,
"variant_title": "Pink",
"vendor": "Apple",
"name": "IPod Nano - Pink",
"gift_card": false,
"price_set": {
"shop_money": {
"amount": "199.99",
"currency_code": "USD"
},
"presentment_money": {
"amount": "173.30",
"currency_code": "EUR"
}
},
"properties": [
{
"name": "custom engraving",
"value": "Happy Birthday Mom!"
}
],
"taxable": true,
"tax_lines": [
{
"title": "HST",
"price": "25.81",
"price_set": {
"shop_money": {
"amount": "25.81",
"currency_code": "USD"
},
"presentment_money": {
"amount": "20.15",
"currency_code": "EUR"
}
},
"rate": 0.13
}
],
"total_discount": "5.00",
"total_discount_set": {
"shop_money": {
"amount": "5.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "4.30",
"currency_code": "EUR"
}
},
"discount_allocations": [
{
"amount": "5.00",
"discount_application_index": 2,
"amount_set": {
"shop_money": {
"amount": "5.00",
"currency_code": "USD"
},
"presentment_money": {
"amount": "3.96",
"currency_code": "EUR"
}
}
}
]
}
]
如果我使用以下代码检索订单项:
Orders <- fromJSON(paste0("https:key:password//@random-shop.myshopify.com/admin/orders.json?status=any&created_at_max=",Sys.Date()-2,"T23:59:59-05:00&limit=250&created_at_min=",Sys.Date()-2,"T00:00:00-05:00&fields=created_at,id,name,total-price,number,line_items"),flatten = T)
Orders <- Orders$orders%>%
flatten()
str(Orders)
订单项将显示为不同长度的数据框的列表。在那些数据框中,字段的范围可以从单个列表到数据框。
不幸的是,这是一个很难完全复制的问题。
有没有办法获取数据帧列表并使每个变量成为其自己的列?
答案 0 :(得分:0)
使用您上面提供的JSON作为名为test.JSON
的文件
导入以获取列表:
library(jsonlite)
orders <- fromJSON("~/Desktop/test.JSON", simplifyVector = F)
# Convert to a dataframe using purrr
library(purrr)
orders2 <- orders %>%
map(unlist) %>%
map_df(bind_rows)
结果:
# A tibble: 1 x 41
fulfillable_qua… fulfillment_ser… fulfillment_sta… grams id price product_id quantity
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 1 amazon fulfilled 500 6697… 199.… 7513594 1
希望这是您需要的-一个数据框,其中每一列都是列表中的一项。