6

I am looking for a way to render a JSON tree using nested <div> as mentioned in the title. Here is a sample of the data (there is a max of 8 levels in the tree):

{
    "children": {
        "Bacteria": {
            "children":{
                "Verrucomicrobia":{
                    "children":{
                        "Methylacidiphilae":{
                            "children":{
                                "Methylacidiphilales":{
                                    "children":{},
                                    "count":2,
                                    "level":"order",
                                    "name":"Methylacidiphilales",
                                    "score":1.46
                                }
                            },
                            "count":2,
                            "level":"class",
                            "name":"Methylacidiphilae",
                            "score":1.46
                        }
                    },
                    "count":2,
                    "level":"phylum",
                    "name":"Verrucomicrobia",
                    "score":1.46
                }
            },
            "count":2,
            "level":"kingdom",
            "name":"Bacteria",
            "score":1.46
        }
    },
    "count":0,
    "level":"root",
    "name":"Root",
    "score":0.0
}

I can get/parse the JSON tree and save it to a variable. Now I need to traverse the tree recursively and either:

  1. Make each node into something that can be rendered as HTML.
  2. Create a new div node and add to a new tree.

But how?

3
  • 1
    I have noted Node.js, but would like to avoid that for now in order to learn some more jQuery. Commented Nov 12, 2012 at 16:11
  • 1
    1. Please edit the question and format the JSON so it visibly resembles what you want. 2. What have you tried so far? Commented Nov 12, 2012 at 16:12
  • I have tried this: pastie.org/5370834. Layout is OK, but the toggle affects the wrong node. I am afraid the code is clumsy. Commented Nov 13, 2012 at 10:46

1 Answer 1

12

You could do this in raw JS with little to no difficulty:

function json2html(json) {
    var i, ret = "";
    ret += "<ul>";
    for( i in json) {
        ret += "<li>"+i+": ";
        if( typeof json[i] === "object") ret += json2html(json[i]);
        else ret += json[i];
        ret += "</li>";
    }
    ret += "</ul>";
    return ret;
}

Just call that function with your object, and it will return the HTML as a set of nested lists - you can of course change it to use just <div>s if you prefer.

EDIT: And here's a version that uses DOM elements and returns a node that can be inserted with appendChild or similar:

function json2html(json) {
    var i, ret = document.createElement('ul'), li;
    for( i in json) {
        li = ret.appendChild(document.createElement('li'));
        li.appendChild(document.createTextNode(i+": "));
        if( typeof json[i] === "object") li.appendChild(json2html(json[i]));
        else li.firstChild.nodeValue += json[i];
    }
    return ret;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.