6
votes

I am struggling to create a nested/hierarchical JSON file. In reality, my file will have varying numbers of children at different levels (from zero children to several), and each "node" in the tree will have the same key:value pairs: name, id, type. Bearing that in mind, my output from R to JSON should appear similar to:

{"name": "I",
 "id": "001",
 "type": "roman",
 "children": [
     {"name": "1",
      "id": "002",
      "type": "arabic", 
      "children": [
          {"name": "A", 
           "id": "003",
           "type": "alpha-U"},
          {"name": "B", 
           "id": "004",
           "type": "alpha-U"}
       ]},
     {"name": "2",
      "id": "005",
      "type": "arabic", 
      "children": [
          {"name": "C", 
           "id": "005",
           "type": "alpha-U"},
          {"name": "D", 
           "id": "006",
           "type": "alpha-U"}
       ]}
]}  

I've tried creating JSON from lists. I know I need a dataframe somewhere in here, but I can't see how to do this.

This code gets me close:

mylist <- list(name="I", id="001", type="roman",
               children=list(name="1", id="002", type="arabic",
                      children=list(name="A", id="003", type="alpha-U")
               ))
jsonlite::toJSON(mylist, pretty=TRUE, auto_unbox=TRUE)

Resulting in this output:

{
  "name": "I",
  "id": "001",
  "type": "roman",
  "children": {
    "name": "1",
    "id": "002",
    "type": "arabic",
    "children": {
      "name": "A",
      "id": "003",
      "type": "alpha-U"
    }
  }
}

The children are not formed properly and I don't see how to get multiple children per level.

I tried this example from SO: How to write to json with children from R but as far as I have been able adapt it, it does not provide the ability to add the key:value pairs at nodes other than the terminal node

Any help to get me to the next steps would be greatly appreciated.

Thanks! Tim

1
How about: mylist <- list( name="I", id="001", type="roman", children=list( list( name="1", id="002", type="arabic" ), list( name="A", id="003", type="alpha-U" ) ) ) ? I think you just want children to be a list of lists.thelatemail
Yes - thank you! This gives me the nesting within [ ] that was I looking for and moves me toward the next step, which is determining how I get from this to a dataframe. I am reverse-engineering from the nested JSON that I need for a D3 visualization. So now I move backward again from this nested list to create an R dataframe. Once I have that, I should be able to move through the steps in the opposite direction: From R dataframe, to nested JSON, to D3 visualization. Similar to points described here: cran.r-project.org/web/packages/tidyjson/vignettes/…Tim
your question seams answered? why you don't write a answer by your self and mark the question as answerd - that would be nice ;)and-bri

1 Answers

3
votes

You can create dataframe first and then assign the frame as a list into cell.

hierarchy1 <- data.frame( name = c("I")
                          , id = c("001")
                          , type = c("roman"))

level1 <- data.frame(name = c("1", "2")
                     , id = c("002", "005")
                     , type = c("arabic", "arabic"))

level2 <- data.frame(name = c("A", "B")
                      , id = c("003","004")
                      , type = c("arabic","arabic"))


level1[1, "children"][[1]] <-   list(level2)
level1[2, "children"][[1]] <-   list(level2)
hierarchy1[1, "children"][[1]] <- list(level1)

write_json(hierarchy1, "yourJson.json")