Skip to content

Instantly share code, notes, and snippets.

@sanxfxteam
Last active August 29, 2015 13:57
Show Gist options
  • Save sanxfxteam/9572451 to your computer and use it in GitHub Desktop.
Save sanxfxteam/9572451 to your computer and use it in GitHub Desktop.
dataframeFromJSON using jsonlite
isNestedDataFrame <- function(df) {
cols = colnames(df)
for (col in 1:ncol(df)) {
c <- df[[cols[col]]]
if (is.data.frame(c)) {
return (TRUE)
}
}
return (FALSE)
}
normalizeDataFrame <- function(df) {
library(stringr)
cols = colnames(df)
for (col in 1:ncol(df)) {
# print(cols[col])
c <- df[[cols[col]]]
if (is.data.frame(c)) {
# Reccursive normalization
if (isNestedDataFrame(c))
c <- normalizeDataFrame(c)
subcols <- colnames(c)
for (subcol in 1:ncol(c)) {
if (subcols[subcol] != 'X') {
newcol <- str_c(cols[col], '.', subcols[subcol])
# print(newcol)
df[newcol] <- (c[subcols[subcol]])
}
}
df <- dropColumns(df, c(cols[col]))
}
}
return (df)
}
dataframeFromJSONFile <- function(filename) {
library(jsonlite)
df <- jsonlite::fromJSON(paste(readLines(filename), collapse=""), simplifyDataFrame=T)
df <- normalizeDataFrame(df)
return (df)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment