问题:我正在尝试使用tidyjson来解析数据框中的json多行。而且我遇到了困难,希望有人可以帮忙。
我要做什么 (1)我希望以每个数组/对象成为具有相应响应的列的方式来解析json。
挑战 (1)代表2个不同人物的每一行的json结构可能彼此略有不同 (2)嵌套json
我尝试过的...但是失败了 我已经尝试了几种方法,但是都行不通。
(1)
jsontest1 %>% # Use the %>% pipe operator to pass json through a pipeline
as.tbl_json %>%
gather_array() %>%
spread_values(user.response = jstring("S"))`
我收到一条错误消息:'collect_array(。)中的错误:2个记录是值而不是数组'
(2)
jsontest1 %>% # Use the %>% pipe operator to pass json through a pipeline
as.tbl_json %>%
gather_keys() %>%
spread_values(
user.response = jstring("S"))`
这是有问题的,因为未解析3个嵌套的json行
我一直在研究stackoverflow(here和here),但似乎无法弄清楚。
数据 Json1
jsontest1 <- '
[{ "currentInvest" : { "S" : "Yes" }, "motivators" : { "L" : [ { "M" : { "option" : { "S" : "I would like to make a positive difference in the world" }, "value" : { "S" : "Slightly motivating" } } }, { "M" : { "option" : { "S" : "Generally, I consider sustainable investments to be less risky investments" }, "value" : { "S" : "Moderately motivating" } } } ] }}
]
'
jston2
jsontest2 <- '
[{ "advisorSustainable" : { "S" : "Not important" }, "approachToInvesting" : { "S" : "I want some small return and would be satisfied just keeping pace with inflation." }, "currentInvest" : { "S" : "No" }, "declineReaction" : { "S" : "Think about changing to more conservative investments but wait and see what happens" }, "doesNotInvest" : { "L" : [ { "S" : "I'm afraid sustainable investments will negatively impact my portfolio's performance" }, { "S" : "I don't know how to invest in sustainable investments" } ] }, "emotionalReaction" : { "S" : "Investment losses make me very uncomfortable. When markets become volatile, I check my portfolio often so I can make adjustments." }, "levelOfInterest" : { "S" : "I'm less interested" }, "motivators" : { "L" : [ { "M" : { "option" : { "S" : "I would like to make a positive difference in the world" }, "value" : { "S" : "Slightly motivating" } } }, { "M" : { "option" : { "S" : "Generally, I consider sustainable investments to be less risky investments" }, "value" : { "S" : "Slightly motivating" } } }, { "M" : { "option" : { "S" : "Generally, I expect sustainable investments to outperform the market" }, "value" : { "S" : "Not at all motivating" } } }, { "M" : { "option" : { "S" : "Aligning my investments with my preferences is important to me" }, "value" : { "S" : "Moderately motivating" } } }, { "M" : { "option" : { "S" : "To keep up with recent trends in investing" }, "value" : { "S" : "Very motivating" } } }, { "M" : { "option" : { "S" : "To help reduce some of the negative impacts of consumption" }, "value" : { "S" : "Extremely motivating" } } } ] }, "otherReasons" : { "S" : "singing" }, "riskTolerance" : { "S" : "Ranging between a 27% loss and 46% gain" }, "selectingInvestments" : { "L" : [ { "M" : { "option" : { "S" : "Fees" }, "value" : { "S" : "Not important" } } }, { "M" : { "option" : { "S" : "Performance" }, "value" : { "S" : "Not important" } } }, { "M" : { "option" : { "S" : "Sustainability or ESG" }, "value" : { "S" : "Extremely important" } } }, { "M" : { "option" : { "S" : "Risk" }, "value" : { "S" : "Pretty important" } } }, { "M" : { "option" : { "S" : "Strength of the management team" }, "value" : { "S" : "Moderately important" } } }, { "M" : { "option" : { "S" : "Located in a hot sector or area" }, "value" : { "S" : "Moderately important" } } }, { "M" : { "option" : { "S" : "The strength of the P/E ratio or other measures of value" }, "value" : { "S" : "Somewhat important" } } } ] }, "volatility" : { "S" : "Agree" }}
]
'