在 R 中分隔三个 JSON 字符和列表数据

Separating three JSON character and list data in R

我有一个 txt 文件,其中包含跨多个参与者的三个主要任务(输入 ID 信息和两个行为任务)的嵌套 JSON 数据。我希望能够完全展平这个文件,并在末尾创建一个 excel 文件,第一个字符串 (rt 33588) 和第二个字符串 (rt 33358 + "success....) 中的展平信息第二个 excel 文件包含第一个字符串 (rt 3358 + "success...)

我试过:

require(pacman)
p_load('tidyverse','jsonlite')

read_file('jsondata.txt') %>%
  str_split('\n') %>% first() %>%
  discard(function(x) x == '') %>%
  map_dfr(fromJSON, flatten=T) -> mydata

然而这个returns错误代码:

Error: Can't combine `..1$responses` <character> and `..2$responses` <list>.

这似乎是一个 tidyverse 错误,我不知道如何解决。如果我手动拆分 txt 文件,我可以 运行 三个字符串中的每一个的上述代码并创建两个 excel 文件,但必须有一种方法来读取单个 txt 文件并拆分是吗?

我的JSON数据如下:

[{"rt":33588,"responses":"{\"School_ID\":\"12345\",\"Class_ID\":\"12345\"}","trial_type":"survey-text-image","trial_index":0,"time_elapsed":33606,"internal_node_id":"0.0-0.0"},{"rt":4424,"responses":"{\"Participant_ID\":\"1833\"}","trial_type":"survey-text-image","trial_index":1,"time_elapsed":38064,"internal_node_id":"0.0-1.0-0.0-0.0"}]
[{"success":true,"timeout":false,"failed_images":[],"failed_audio":[],"failed_video":[],"trial_type":"preload","trial_index":0,"time_elapsed":152,"internal_node_id":"0.0-0.0"},{"rt":6004,"stimulus":"<div><h1> Task 1 </h1></div></div>","response":0,"trial_type":"html-button-response-modified","trial_index":1,"time_elapsed":6172,"internal_node_id":"0.0-1.0"},{"responses":[{"image_id":"p_ts1","image":"img/target.png","rt":1062,"correct":true,"coord_x":"501","coord_y":"205","target":"true","side":"C","height":"34.5","width":"33.7"},{"image_id":"p_ts2","image":"img/target.png","rt":1444,"correct":true,"coord_x":"501","coord_y":"258","target":"true","side":"C","height":"34.5","width":"33.7"},{"image_id":"p_ts3","image":"img/target.png","rt":2177,"correct":true,"coord_x":"501","coord_y":"507","target":"true","side":"C","height":"34.5","width":"33.7"}],"trial_type":"target-cancellation","trial_index":2,"time_elapsed":10108,"internal_node_id":"0.0-2.0-0.0"},{"rt":1003,"stimulus":"<div><h1> Main Task</h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":3,"time_elapsed":11123,"internal_node_id":"0.0-3.0"},{"responses":[{"image_id":"ts1","image":"img/target.png","rt":1264,"correct":true,"coord_x":"932","coord_y":"39","target":"true","side":"R","height":"34.5","width":"33.7"},{"image_id":"ts2","image":"img/target.png","rt":1484,"correct":true,"coord_x":"880","coord_y":"77","target":"true","side":"R","height":"34.5","width":"33.7"},{"image_id":"ts11","image":"img/target.png","rt":2039,"correct":true,"coord_x":"744","coord_y":"53","target":"true","side":"R","height":"34.5","width":"33.7"}],"trial_type":"target-cancellation","trial_index":4,"time_elapsed":27258,"internal_node_id":"0.0-4.0"},{"rt":1713.0000000000036,"stimulus":"<div><h1> Finish Task</h1><img id='stars' src='img/target.png'></img></div>","response":0,"trial_type":"html-button-response-modified","trial_index":5,"time_elapsed":28984,"internal_node_id":"0.0-5.0"}]
[{"success":true,"timeout":false,"failed_images":[],"failed_audio":[],"failed_video":[],"trial_type":"preload","trial_index":0,"time_elapsed":156,"internal_node_id":"0.0-0.0"},{"rt":1794,"stimulus":"<div><h1> Task 2</h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":1,"time_elapsed":1959,"internal_node_id":"0.0-1.0"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":2,"time_elapsed":5311,"internal_node_id":"0.0-2.0-0.0"},{"rt":1082.999999999999,"stimulus":"img/target.png","response":1,"test_part":"Practice Test","condition":"Medium","type":"Large","target":"two","correct_response":1,"trial_type":"image-button-response","trial_index":3,"time_elapsed":6404,"internal_node_id":"0.0-2.0-1.0","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":4,"time_elapsed":7411,"internal_node_id":"0.0-2.0-2.0"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":5,"time_elapsed":13421,"internal_node_id":"0.0-2.0-0.1"},{"rt":971,"stimulus":"img/SmallSun.png","response":0,"test_part":"Practice Test","condition":"Extra Long","type":"Small","target":"left","correct_response":0,"trial_type":"image-button-response","trial_index":6,"time_elapsed":14421,"internal_node_id":"0.0-2.0-1.1","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":7,"time_elapsed":15429,"internal_node_id":"0.0-2.0-2.1"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":8,"time_elapsed":16577,"internal_node_id":"0.0-2.0-0.2"},{"rt":1000,"stimulus":"img/SmallStar.png","response":1,"test_part":"Practice Test","condition":"Short","type":"Small","target":"right","correct_response":1,"trial_type":"image-button-response","trial_index":9,"time_elapsed":17587,"internal_node_id":"0.0-2.0-1.2","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":10,"time_elapsed":18594,"internal_node_id":"0.0-2.0-2.2"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"practice_fixation","trial_type":"image-button-response","trial_index":11,"time_elapsed":21369,"internal_node_id":"0.0-2.0-0.3"},{"rt":1037,"stimulus":"img/MediumSun.png","response":0,"test_part":"Practise Test","condition":"Medium","type":"Medium","target":"right","correct_response":0,"trial_type":"image-button-response","trial_index":12,"time_elapsed":22415,"internal_node_id":"0.0-2.0-1.3","correct":true},{"rt":null,"stimulus":"img/FeedbackTick.png","response":null,"trial_type":"image-button-response","trial_index":13,"time_elapsed":23428,"internal_node_id":"0.0-2.0-2.3"},{"rt":1621,"stimulus":"<div><h1> Main Task </h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":20,"time_elapsed":33659,"internal_node_id":"0.0-3.0"},{"rt":null,"stimulus":"img/FixationCross.png","response":null,"test_part":"fixation","trial_type":"image-button-response","trial_index":21,"time_elapsed":37990,"internal_node_id":"0.0-4.0-0.0"},{"rt":998,"stimulus":"img/target.png","response":0,"test_part":"test","condition":"Medium","type":"Medium","target":"left","correct_response":0,"trial_type":"image-button-response","trial_index":22,"time_elapsed":39004,"internal_node_id":"0.0-4.0-1.0","correct":true},{"rt":886,"stimulus":"img/target.png","response":0,"test_part":"test","condition":"Long","type":"Medium","target":"left","correct_response":0,"trial_type":"image-button-response","trial_index":80,"time_elapsed":147593,"internal_node_id":"0.0-4.0-1.29","correct":true},{"rt":1369,"stimulus":"<div><h1> End Task Race </h1></div>","response":0,"trial_type":"html-button-response-modified","trial_index":99,"time_elapsed":180056,"internal_node_id":"0.0-5.0"}]

非常感谢任何帮助 - 我通常不使用 JSON 数据,所以任何指导都非常有帮助!

错误是由于您的文本数据包含 3 个具有不同结构的 json,因此当使用 map_dfr 进行映射时,它们会导致 3 个结构略有不同的 df,而 map_dfr 无法做到结合他们。在这种情况下,第一个 json 字符串中的 responses 列是字符,而第二个 json responses 是一个列表

library('tidyverse')
library('jsonlite')

text_data <- read_lines('jsondata.txt')

df_1 <- fromJSON(text_data[1], flatten = TRUE)
class(df_1$responses)
#> [1] "character"

df_2 <- fromJSON(text_data[2], flatten = TRUE)
class(df_2$responses)
#> [1] "list"

如果你只需要一起读取数据而不是将它们放在一起data.frame那么你可以这样做

library('tidyverse')
library('jsonlite')

text_data <- read_lines('jsondata.txt')

json_data <- map(text_data, fromJSON, flatten = TRUE)
summary(json_data)
#>      Length Class      Mode
#> [1,]  6     data.frame list
#> [2,] 13     data.frame list
#> [3,] 18     data.frame list

reprex package (v2.0.0) 于 2021-05-20 创建 reprex package (v2.0.0)

于 2021-05-20 创建