R on JSON,嵌套特里克斯 [英] R on JSON, nested triks
问题描述
您能建议我如何读取以下.json并仅提取某些字段,例如下面的示例吗?我需要分别粘贴文件的实际内容.我试图将其送入fromJSON()
并下沉,但失败了,因为b'z特殊字符(?).这是我的测试文件:
Can you advise me how I can read the following .json and extract just certain fields like in sample below? I need to paste actual content of file separately. I tried to feed it into fromJSON()
and sink but it failed, b'z special chars(?). This is my test file:
{"batch_date": "2015-05", "name": "Jeff Macronsh", "cust_cid": "001555", "clients": ["111112222", "1324132531", "1235325", "1324324321"], "fans": 2319, "rewards": 3.75, "type": "dealer", "bonuses": {"suka": 13, "plain": 4, "writer": 1, "maxima": 1, "more": 1, "prima": 5}, "lexus": []}
{"batch_date": "2014-07", "name": "NWest", "cust_cid": "332224", "clients": ["093485734250"], "fans": 1, "rewards": 4.5, "type": "dealer", "bonuses": {"note": 12, "suv": 10, "prima": 1}, "lexus": []}
{"batch_date": "2014-11", "name": "Muhhamed Karne", "cust_cid": "234566000", "bonuses": {"profile": 5, "suv": 52, "cute": 1, "plain": 43, "bbb": 35, "note": 33, "photos": 3, "maxima": 56, "more": 12, "prima": 151}, "lexus": [2013, 2014]}
{"batch_date": "2013-11", "name": "West", "cust_cid": "4567465800", "bonuses": {"plain": 1, "maxima": 1, "more": 2, "photos": 1, "suv": 1}, "lexus": []}
{"batch_date": "2014-02", "name": "Jake", "cust_cid": "6467889000", "bonuses": {"cute": 1, "suv": 30, "plain": 43, "writer": 38, "note": 16, "photos": 2, "maxima": 33, "prima": 39, "more": 5}, "lexus": [2012, 2014, 2015]}
{"batch_date": "2014-11", "name": "Michelle Mow", "cust_cid": "345653477", "bonuses": {"maxima": 1, "write": 15, "platinum": 33}, "lexus": []}
{"batch_date": "2015-07", "name": "NWest", "cust_cid": "332224", "clients": ["093485734250", "4313124324"], "bonuses": {"note": 12, "suv": 90, "prima": 1}, "lexus": []}
我只想提取
name cust_id suv
NWest 332224 90
我尝试使用此代码,但仅获得第一行.在我的第7个案例中有6个(一个没有suv),我如何才能全部使用这些代码?
I tried to use this code but get only first row. How I can all of them in my case 6 of 7 (one doesn't have suv)?
library("rjson")
json_file <- "test.json"
json_data <- fromJSON(file= json_file)
a <- c(json_data$name, "suv", json_data$cust_cid, json_data$bonuses$suka)
推荐答案
好的,很多步骤: s 首先,将JSON应用于每行. 然后,列出大多数变量,它们是简单的值 然后取出作为子数据帧的奖金,并将其合并.
Ok, a number of steps: s First, lapply from JSON to each line. Then, put out most variables which are simple values Then pull out bonuses, which are subdataframes, and merge them in.
为客户准备的一小段额外内容,仅用于踢球.
A small extra section for clients, just for kicks.
然后只需选择所需的变量. 一个
Then just select the variables you want. a
library(rjson)
library(rlist)
library(magrittr)
library(dplyr)
list_structure =
"test.json" %>%
readLines %>%
lapply(fromJSON)
transaction =
list_structure %>%
lapply(. %>%
list.remove(c("bonuses", "lexus", "clients")) %>%
as.data.frame) %>%
bind_rows(.id = "ID") %>%
full_join(
list_structure %>%
lapply(. %>%
use_series(bonuses) %>%
as.data.frame) %>%
bind_rows(.id = "ID"))
clients =
list_structure %>%
setNames(seq_along(.)) %>%
lapply(. %>% use_series(clients) ) %>%
Filter(. %>% is.null %>%`!`, .) %>%
lapply(. %>% {data_frame(client = .)}) %>%
bind_rows(.id = "ID") %>%
mutate(ID = as.numeric(ID))
result =
transaction %>%
select(name, cust_cid, suv) %>%
filter(!is.na(suv))
这篇关于R on JSON,嵌套特里克斯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!