GithubHelp home page GithubHelp logo

senthilsivanath / json2html-list Goto Github PK

View Code? Open in Web Editor NEW

This project forked from suskind/json2html-list

0.0 1.0 0.0 6 KB

Converts JSON to HTML UL-LI list

License: MIT License

JavaScript 73.90% HTML 26.10%

json2html-list's Introduction

json2html-list

Converts JSON to HTML UL-LI list

Install

Bower

bower install --save json2html-list 
<script src="./bower_components/json2html-list/index.js"></script>

Npm

npm install --save json2html-list
var JSON2HTMLList = require('json2html-list');

How To:

<div id="output">
</div> 

<script>

var jsonObj = {
    prop1: 'value 1',
    prop_obj: {
        prop2: 'value 2',
        prop3: 3,
        prop4: [
            'item 5',
            'item 6'
        ]
    }
};

var html = JSON2HTMLList(jsonObj);

document.getElementById('output').appendChild(html);

</script>

Options

  • container: (default: div) tag name to list container, this container will be returned
  • formatContainer: function to format container element. Will receive container element as parameter
  • formatUl: function to format each UL element. Will receive UL elements as parameter
  • formatLi: function to format each LI element. Will receive LI elements as parameter
  • formatProperty: function to format each found object property. Will receive a object property as text node
  • formatValue: function to format each object or array value. Will receive the value as text node and the correspondent property (if array will receive array index)

How To with Options:

<div id="output">
</div> 

<script>

var jsonObj = {
    prop1: 'value 1',
    prop_obj: {
        prop2: 'value 2',
        prop3: 3,
        propPre: 'This \nwill be \n a pre',
        prop4: [
            'item 5',
            'item 6'
        ]
    }
};

var options = {
    formatContainer: function(cont) {
        cont.className = 'my-container-class';
        return cont;
    }, 
    formatUl: function(ul) {
        ul.className = 'my-ul-class';
        return ul;
    },
    formatLi: function(li) {
        li.className = 'my-li-class';
        return li;
    },
    formatProperty: function(prop) {
        var strong = document.createElement('strong');
        strong.appendChild(prop);
        strong.appendChild(document.createTextNode(': '));

        return strong;
    },
    formatValue: function(value, prop) {
        var elm;
        if(prop === 'propPre') {
            elm = document.createElement('pre');
        } else {
            elm = document.createElement('span');
        }
        elm.appendChild(value);
    
        return elm; 
    }
};

var html = JSON2HTMLList(jsonObj, options);

document.getElementById('output').appendChild(html);

</script>

Converts this JSON

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": [
                            "GML",
                            "XML"
                        ]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

Into this HTML

<div class="container">
    <ul class="ul-item">
        <li class="li-item"><strong>glossary</strong>
            <ul class="ul-item">
                <li class="li-item"><strong>title</strong><span>example glossary</span></li>
                <li class="li-item"><strong>GlossDiv</strong>
                    <ul class="ul-item">
                        <li class="li-item"><strong>title</strong><span>S</span></li>
                        <li class="li-item"><strong>GlossList</strong>
                            <ul class="ul-item">
                                <li class="li-item"><strong>GlossEntry</strong>
                                    <ul class="ul-item">
                                        <li class="li-item"><strong>ID</strong><span>SGML</span></li>
                                        <li class="li-item"><strong>SortAs</strong><span>SGML</span></li>
                                        <li class="li-item"><strong>GlossTerm</strong><span>Standard Generalized Markup Language</span></li>
                                        <li class="li-item"><strong>Acronym</strong><span>SGML</span></li>
                                        <li class="li-item"><strong>Abbrev</strong><span>ISO 8879:1986</span></li>
                                        <li class="li-item"><strong>GlossDef</strong>
                                            <ul class="ul-item">
                                                <li class="li-item"><strong>para</strong><span>A meta-markup language, used to create markup languages such as DocBook.</span></li>
                                                <li class="li-item"><strong>GlossSeeAlso</strong>
                                                    <ul class="ul-item">
                                                        <li class="li-item"><span>GML</span></li>
                                                        <li class="li-item"><span>XML</span></li>
                                                    </ul>
                                                </li>
                                            </ul>
                                        </li>
                                        <li class="li-item"><strong>GlossSee</strong><span>markup</span></li>
                                    </ul>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </li>
    </ul>
</div>

json2html-list's People

Contributors

suskind avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.