Skip to content

python module for converting complex JSON oject to HTML Table representation

License

Notifications You must be signed in to change notification settings

xprudhomme/json2html

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json2html

Python wrapper to convert JSON into a human readable HTML Table representation.

Latest Version Downloads Build

Features

  1. User-friendly Table fomat - easy to read and show.
  2. If any value of some key is array of objects, and all keys of all those objects are same, it will auto-club them instead of creating a new row for each Object within Array. For eg: `jsonObject = {"sampleData": [ {"a":1, "b":2, "c":3}, {"a":5, "b":6, "c":7} ] }`
  3. Easy to style for different purposes. Pass table attributes so that generated Table can have custom attributes like class, etc..

Live Demo

Visit Online json2html Convertor

Installation

$ pip install json2html

Or, Download here and run python setup.py install after changing directory to /json2html

Example Usage

Example 1: Basic usage

from json2html import *
json2html.convert(json = {'name':'softvar','age':'22'})

Output:

<table border="1"><tr><th>age</th><td>22</td></tr><tr><th>name</th><td>softvar</td></tr></table>
age 22
name softvar

Example 2: Setting custom attributes to table

from json2html import *
json2html.convert(json = {'name':'softvar','age':'22'}, table_attributes="class=\"table table-bordered table-hover\"")

Output:

<table class="table table-bordered table-hover"><tr><th>age</th><td>22</td></tr><tr><th>name</th><td>softvar</td></tr></table>

Example 3: Clubbing same keys of: Array of Objects

from json2html import *
_json2conv = {"sample": [ {"a":1, "b":2, "c":3}, {"a":5, "b":6, "c":7} ] }
json2html.convert(json = _json2conv)

Output:

<table border="1"><tr><th>sample</th><td><table border="1"><tr><th>a</th><th>c</th><th>b</th></tr><tr><td>1</td><td>3</td><td>2</td></tr><tr><td>5</td><td>7</td><td>6</td></tr></table></td></tr></table>
a c b
1 3 2
5 7 6

Example 4: Each row for different key(s) of: Array of Objects

from json2html import *
_json2conv = {"sample": [ {"a":1, "b":2, "c":3}, {"1a1":5, "1b1":6, "c":7} ] }
json2html.convert(json = _json2conv)

Output:

<table border="1"><tr><th>sample</th><td><ul><li><table border="1"><tr><th>a</th><td>1</td></tr><tr><th>c</th><td>3</td></tr><tr><th>b</th><td>2</td></tr></table></li><li><table border="1"><tr><th>1b1</th><td>6</td></tr><tr><th>c</th><td>7</td></tr><tr><th>1a1</th><td>5</td></tr></table></li></ul></td></tr></table>

Example 5: [Source: json.org/example]

from json2html import *

_json2conv = {
        "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"
                                }
                        }
                }
        }
}

json2html.convert(json = _json2conv)

Output:

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

Tests

cd test/
python run_tests.py

Contributors

  1. Michel Müller: @muellermichel
    • patch #2
    • Added support for clubbing Array of Objects with same keys, more readable format.
    • Added support for adding custom table_attributes.

Patches are highly welcomed.

About

python module for converting complex JSON oject to HTML Table representation

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Python 100.0%