Create nested/hierarchical JSON with R and JSONLITE?
Asked Answered
E

1

10

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

Eby answered 5/4, 2017 at 23:38 Comment(3)
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.Griseldagriseldis
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/…Eby
your question seams answered? why you don't write a answer by your self and mark the question as answerd - that would be nice ;)Adamite
M
4

You can create the dataframes first and then assign the frame as a list into the cell like so:

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")
Mcchesney answered 10/1, 2020 at 13:49 Comment(1)
This is much too complicated if you want to process larger nested itemsDisinterest

© 2022 - 2024 — McMap. All rights reserved.