brent
brent

Reputation:

Convert JSON to HTML Tree

I would like to generate an HTML tree (preferably UL-LI) from the JSON example below. Does anyone have a simple, recursive JS function (not a framework) that can handle this specific structure? Thanks for your help!

{ "folder" : [ {
    "title" : "1",
    "folder" : [ {
        "title" : "1.1",
        "folder" : [ {
            "title" : "1.1.1",
        } , {
            "title" : "1.1.2",
        } ]
    } ]
} , {
    "title" : "2",
} ] }

Upvotes: 13

Views: 32970

Answers (7)

Abhay Katheria
Abhay Katheria

Reputation: 19

I have made a pip package for the same do check it out JSON2tree. Install it using pip install json2tree then use cli tool to create HTML tree. json2tree -j example.json -o output.html

Upvotes: 0

Boldewyn
Boldewyn

Reputation: 82734

function to_ul (obj) {
  // --------v create an <ul> element
  var f, li, ul = document.createElement ("ul");
  // --v loop through its children
  for (f = 0; f < obj.folder.length; f++) {
    li = document.createElement ("li");
    li.appendChild (document.createTextNode (obj.folder[f].title));
    // if the child has a 'folder' prop on its own, call me again
    if (obj.folder[f].folder) {
      li.appendChild (to_ul (obj.folder[f].folder));
    }
    ul.appendChild (li);
  }
  return ul;
}

Caveat: No error checking! If a 'title' or 'folder' is missing, the whole thing could blow up.

Upvotes: 8

Dexygen
Dexygen

Reputation: 12561

I had a problem getting my browser to accept the data structure submitted by the OP, but here is a fully working example I've drawn up for my own, similar purposes. Beside the function I provide the data structure as well, with name/branches instead of title/folder.

function to_ul(branches) {
  var ul = document.createElement("ul");

  for (var i = 0, n = branches.length; i < n; i++) {
    var branch = branches[i];
    var li = document.createElement("li");

    var text = document.createTextNode(branch.name);
    li.appendChild(text);

    if (branch.branches) {
      li.appendChild(to_ul(branch.branches));
    }

    ul.appendChild(li);
  }

  return ul;
}

function renderTree() {
  var treeEl = document.getElementById("tree");

  var treeObj = {
    "root": [{
      "name": "George & Sarah Trede",
      "branches": [{
        "name": "George & Frances Trede",
        "branches": [{
          "name": "Mary (Trede) Jempty"
        }, {
          "name": "Carol (Trede) Moeller"
        }]
      }, {
        "name": "Mary (Trede) Sheehan"
      }, {
        "name": "Ward Trede"
      }]
    }]
  };

  treeEl.appendChild(to_ul(treeObj.root));
}

renderTree()
<div id="tree"></div>

Upvotes: 7

Chad
Chad

Reputation: 201

Check out the jquery plugin JSON2HTML, it's a little simpler to use than PURE and I've used it on a couple of site's I've created.

http://json2html.com

Upvotes: 2

Dimitris Moraitis
Dimitris Moraitis

Reputation: 31

function to_li(obj, name) {
    var li = document.createElement("li");
    if (typeof(name) != "undefined") {
        var strong = document.createElement("strong");
        strong.appendChild(document.createTextNode(name + ": "));
        li.appendChild(strong);
    }
    if (typeof(obj) != "object"){
        li.appendChild(document.createTextNode(obj));
    } else {
        var ul = document.createElement ("ul");
        for (var prop in obj){
            ul.appendChild(to_li(obj[prop],prop));
        }
        li.appendChild(ul);
    }
    return li;
}

Upvotes: 3

Surya
Surya

Reputation: 4972

I've used PURE with some success in the past for this kind of thing.

Upvotes: 3

zacharyliu
zacharyliu

Reputation: 26308

@Boldewyn: I believe you can also use a For...In loop instead of a regular For loop to shorten the code a bit. Of course, I don't have much experience using this kind of loop, so please check my code snippet.

for (var i in obj.folder) {
    li = document.createElement ("li");
    li.appendChild (document.createTextNode (i.title));
    // if the child has a 'folder' prop on its own, call me again
    if (i.folder) {
      li.appendChild (to_ul (i.folder));
    }
}

Upvotes: 0

Related Questions