Ich habe eine Liste von mehr als 100.000 JSON-Dateien, von denen ich eine data.table mit nur wenigen Variablen erhalten möchte. Leider sind die Dateien komplex. Der Inhalt jeder JSON-Datei wie folgt aussieht:Von der Liste der JSON-Dateien zu data.table: partielle Variablenliste
Beispiel 1
$id
[1] "10.1"
$title
$title$value
[1] "Why this item"
$itemsource
$itemsource$id
[1] "AA"
$date
[1] "1992-01-01"
$itemType
[1] "art"
$creators
list()
Probe 2
$id
[1] "10.2"
$title
$title$value
[1] "We need this item"
$itemsource
$itemsource$id
[1] "AY"
$date
[1] "1999-01-01"
$itemType
[1] "art"
$creators
type name firstname surname affiliationIds
1 Person Frank W. Cornell. Frank W. Cornell. a1
2 Person David A. Chen. David A. Chen. a1
$affiliations
id name
1 a1 Foreign Affairs Desk, New York Times
Was ich von diesem Satz von Dateien benötigen, ist eine Tabelle mit Schöpfer Namen, Element-IDs und Daten . Für die beiden Beispieldateien über:
library(parallel)
library(data.table)
library(jsonlite)
library(dplyr)
filelist = list.files(pattern="*.json",recursive=TRUE,include.dirs =TRUE)
parsed = mclapply(filelist, function(x) fromJSON(x),mc.cores=24)
data = rbindlist(mclapply(1:length(parsed), function(x) {
a = data.table(item = parsed[[x]]$id, date = list(list(parsed[[x]]$date)), name = list(list(parsed[[x]]$name)), creatortype = list(list(parsed[[x]]$creatortype))) #ignoring the firstname/lastname fields here for convenience
b = data.table(id = a$item, date = unlist(a$date), name=unlist(a$name), creatortype=unlist(a$creatortype))
return(b)
},mc.cores=24))
jedoch auf dem letzten Schritt, bekomme ich diesen Fehler:
id date name firstname lastname creatortype
"10.1" "1992-01-01" NA NA NA NA
"10.2" "1999-01-01" Frank W. Cornell. Frank W. Cornell. Person
"10.2" "1999-01-01" David A. Chen. David A. Chen. Person
Was ich bisher getan haben
"Error in rbindlist(mclapply(1:length(parsed), function(x){:
Item 1 of list is not a data.frame, data.table or list"
Vielen Dank im Voraus für deine Vorschläge. Verwandte Fragen sind: Extract data from list of lists [R] R convert json to list to data.table I want to convert JSON file into data.table in r How can read files from directory using R? Convert R data table column from JSON to data table