Last active
August 29, 2015 13:57
-
-
Save sanxfxteam/9572451 to your computer and use it in GitHub Desktop.
dataframeFromJSON using jsonlite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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