How to convert JSON into xlsx file using R

Viewed 141

I want to convert a JSON into xlsx file using R. Here is the scenario that I am expecting:

Here is my R object.

    myObject <- {
  "Alabama" : { 
                "Hoover": {
                           "x":[33.40556, 33.40565, 33.40555],
                           "y":[-86.81111, -86.81314, -86.81343]
                          },
                "Dothan": {
                           "x":[31.22722],
                           "y":[-85.40722]
                          },
                  ...
              },
  "Alaska" : {
               "Chugiak" : {
                            "x":[61.38778],
                            "y":[-149.48139]
                           },
               ...
             }
  ...
}

Here is the result that I want in xlsx:

Alabama Hoover 33.40556 -86.81111
Alabama Hoover 33.40565 -86.81314
Alabama Hoover 33.40555 -86.81343
Alabama Dothan 31.22722 -85.40722
Alabama Gadsden 34.01028 -86.01028
Alaska Chugiak 61.38778 -149.48139
...

P.S : Can I get the same result from an R object that is auto_unboxed such as:

myObject <- {
  "Alabama" : { 
                "Hoover": {
                           "x":[33.40556, 33.40565, 33.40555],
                           "y":[-86.81111, -86.81314, -86.81343]
                          },
                "Dothan": {
                           "x":31.22722,
                           "y":-85.40722
                          },
                  ...
              },
  "Alaska" : {
               "Chugiak" : {
                            "x":61.38778,
                            "y":-149.48139
                           },
               ...
             }
  ...
}
1 Answers

Try this:

myfunc <- function(o) {
  if ("x" %in% names(o)) {
    as.data.frame(o)
  } else {
    x <- lapply(o, function(z) myfunc(z))
    do.call(rbind.data.frame, Map(cbind.data.frame, nm=names(x), x))
  }
}
out <- myfunc(obj)
names(out) <- make.names(names(out), unique = TRUE)
out
#                       nm    nm.1        x          y
# Alabama.Hoover.1 Alabama  Hoover 33.40556  -86.81111
# Alabama.Hoover.2 Alabama  Hoover 33.40565  -86.81314
# Alabama.Hoover.3 Alabama  Hoover 33.40555  -86.81343
# Alabama.Dothan   Alabama  Dothan 31.22722  -85.40722
# Alaska            Alaska Chugiak 61.38778 -149.48139

The row names (Alabama.Hoover.3) are disposable. The repeat name columns should be unique (that's what make.names is doing, otherwise they would all be named "nm").

Data:

obj <- jsonlite::fromJSON('{
  "Alabama" : { 
                "Hoover": {
                           "x":[33.40556, 33.40565, 33.40555],
                           "y":[-86.81111, -86.81314, -86.81343]
                          },
                "Dothan": {
                           "x":[31.22722],
                           "y":[-85.40722]
                          }
              },
  "Alaska" : {
               "Chugiak" : {
                            "x":[61.38778],
                            "y":[-149.48139]
                           }
             } }')
Related