Skip to content

Instantly share code, notes, and snippets.

@retrography
Last active February 26, 2021 23:09
Show Gist options
  • Save retrography/fa622bb57d1ca25ecef8 to your computer and use it in GitHub Desktop.
Save retrography/fa622bb57d1ca25ecef8 to your computer and use it in GitHub Desktop.
JSON to CSV convertor. Uses `jsonlite` R package, flattens all the hierarchical structure and converts all remaining lists/arrays into strings.
#!/usr/bin/Rscript
if (!require(jsonlite, quietly = TRUE, warn.conflicts = FALSE)) {
stop("This program requires the `jsonlite` package. Please install it by running `install.packages('jsonlite')` in R and then try again.")
}
args <- commandArgs(trailingOnly = TRUE)
if (length(args) == 0) {
input <- readLines(file("stdin"))
} else if (args[[1]] == "--help") {
write("Usage: json2csv <json input file> <csv output file> \nAlternatively, you can redirect inputs and outputs using pipes, stdin and stdout.",
stderr())
q()
} else {
input <- readLines(args[[1]])
}
df <- fromJSON(input, flatten = TRUE)
listcols <- colnames(df[lapply(colnames(df), function(x) class(df[,x])) == "list"])
for (col in listcols) df[col] = data.frame(unlist(lapply(df[,col], function(x) toString(unlist(x)))))
if (length(args) > 1) {
write.csv(df, args[[2]], row.names = FALSE)
} else {
write.csv(df, stdout(), row.names = FALSE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment