JSON to HTML Table Representation
Project description
Python wrapper to convert JSON into a human readable HTML Table representation.
Features
User-friendly Table fomat - easy to read and show.
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} ] }`
Easy to style for different purposes. Pass table attributes so that generated Table can have custom attributes like class, etc..
Live Demo
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
- 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.
Better error message on bad call of ‘convert’
Patches are highly welcomed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file json2html-1.0.2.tar.gz
.
File metadata
- Download URL: json2html-1.0.2.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
2538d17a68cb887042b9987c91620028454a2cd7e9a0ba980f285ab321a15881
|
|
MD5 |
a24151fdf4761cfab93ed64a2218296a
|
|
BLAKE2b-256 |
06c172ff9e5aef322580cb4abe03d413f061a93a03af1d45ec3dc0e336cf8438
|