• Stars
    star
    274
  • Rank 150,274 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created about 11 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🐍 Python module for converting complex JSON to HTML Table representation

json2html

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

Latest Version Downloads Build

Features

  • User friendly tablular fomat, easy to read and share.
  • If value of the key is array of objects and all the keys are same(value of the key is a dict of list), the module will club by default. Eg.
input = {
        "sampleData": [{
                "a":1, "b":2, "c":3
        }, {
                "a":5, "b":6, "c":7
        }]
}

will create only one row combining the results. This feature can be turned off by explicitly passing an argument ``clubbing = False``.
  • Generated table can be provided some attributes explicitly. Eg. giving an id, class or any data-* attribute.
  • Python 3 compatible

Live Demo

Click here for the online demo.

List of valid arguments

json2html.convert - The module's convert method accepts the following arguments:

Argument Description
json a valid JSON; This can either be a string in valid JSON format or a python object that is either dict-like or list-like at the top level.
table_attributes e.g. pass id="info-table" or class="bootstrap-class"/data-* to apply these attributes to the generated table
clubbing turn on[default]/off clubbing of list with same keys of a dict / Array of objects with same key
encode turn on/off[default] encoding of result to escaped html, compatible with any browser
escape turn on[default]/off escaping of html tags in text nodes (prevents XSS attacks in case you pass untrusted data to json2html)

Installation

$ pip install json2html

Or, Download [here](https://github.com/softvar/json2html/releases) and run python setup.py install after changing directory to /json2html

Example Usage

Example 1: Basic usage

from json2html import *
input = {
        "name": "json2html",
        "description": "Converts JSON to HTML tabular representation"
}
json2html.convert(json = input)

Output:

<table border="1"><tr><th>name</th><td>json2html</td></tr><tr><th>description</th><td>converts JSON to HTML tabular representation</td></tr></table>
name json2html
description Converts JSON to HTML tabular representation

Example 2: Setting custom attributes to table

from json2html import *
input = {
        "name": "json2html",
        "description": "Converts JSON to HTML tabular representation"
}
json2html.convert(json = input, table_attributes="id=\"info-table\" class=\"table table-bordered table-hover\"")

Output:

<table id="info-table" class="table table-bordered table-hover"><tr><th>name</th><td>json2html</td></tr><tr><th>description</th><td>Converts JSON to HTML tabular representation</td></tr></table>

Example 3: Clubbing same keys of: Array of Objects

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

Output:

<table border="1"><tr><th>sample</th><td><table border="1"><thead><tr><th>b</th><th>c</th><th>a</th></tr></thead><tbody><tr><td>2</td><td>3</td><td>1</td></tr><tr><td>6</td><td>7</td><td>5</td></tr></tbody></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 *
input = {
        "sample": [{
                "a":1, "b":2, "c":3
        }, {
                "1a1":5, "1b1":6, "c":7
        }]
}
json2html.convert(json = input)

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 *

input = {
        "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 = input)

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

Tested with Python 2.7 and 3.5+.

Contributors

  1. Michel Mueller: [@muellermichel](https://github.com/muellermichel)
    • Added support for clubbing Array of Objects with same keys, more readable format.
    • Added support for adding custom table_attributes.
    • Convert now accepts unicode and bytestrings for the keyword argument "json".
    • Output now should always appear in the same order as input.
    • Now supports JSON Lists (at top level), including clubbing.
    • Now supports empty inputs and positional arguments for convert.
    • Python 3 support ; Added integration tests for Python 2.6, 3.4 and 3.5 such that support doesn't break.
    • Can now also do the proper encoding for you (disabled by default to not break backwards compatibility).
    • Can now handle non-JSON objects on a best-effort principle.
    • Now by default escapes html in text nodes to prevent XSS attacks.
  2. Daniel Lekic: [@lekic](https://github.com/lekic)
    • Fixed issue with one-item lists not rendering correctly.
    • General code cleanup, fixed all naming conventions and coding standards to adhere to PEP8 conventions.
  3. Kyle Smith: [@smithk86](https://github.com/smithk86)
    • Added thead and tbody tags to group header and content rows when creating a table from an array of objects.

Copyright and License

The MIT license

Copyright (c) 2013-2021 Varun Malhotra

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

enhanced-github

πŸš€ Browser extension to display size of each file, download link and copy file contents directly to the clipboard
JavaScript
2,098
star
2

secure-ls

πŸ”’ Secure localStorage data with high level of encryption and data compression
JavaScript
645
star
3

awesome-web-storage

😎 Everything you need to know about Client-side Storage.
443
star
4

awesome-startups

❀️ A curated list of awesome startups
PHP
244
star
5

translatr

πŸ’¬ Translate to multiple languages at once
HTML
157
star
6

super-workers

🐴 Distribute load on front-end via parallelism
JavaScript
95
star
7

json2html-flask

🐍 Webapp to convert complex JSON Object to HTML representation
HTML
94
star
8

simplegist

Advanced python wrapper for Github Gist API
Python
64
star
9

producthunt_globe

🌎 A visualization of ProductHunt users throughout the globe
JavaScript
27
star
10

awesome-products-by-women

πŸ‘­ Awesome products made by female founders
JavaScript
23
star
11

ns2-roadv

roadv protocol implementation in ns2
C++
21
star
12

softvar.github.io

My new website using materializecss framework
HTML
21
star
13

save-the-forest

πŸ”₯ Canvas based HTML5 game where a player has to extinguish fire on trees using his magical water tank in order to save the Burning Forest.
JavaScript
18
star
14

colorschemr

🎨 Color Palettes one will fall in love with <3
HTML
15
star
15

dont_risk_your_life

Save the LOST man
JavaScript
9
star
16

new-site-engine

My site's engine using hyde and materialize framework
CSS
9
star
17

pyShare

It's a local file sharing app in python(GUI with Kivy)
Python
8
star
18

frontend-boilerplate

Frontend application scaffolding to start immediate development ❀️
JavaScript
8
star
19

js

Every possible javascript utility you ever need
JavaScript
6
star
20

fOS-Gisthub

Managing Github-Gists in firefox OS(smartphone)
JavaScript
4
star
21

resumizr

Real time Resume Builder, auto-filling data from connected social networking sites
JavaScript
3
star
22

bridge-the-gap

Entry game for js13kGames 2015
CSS
3
star
23

goldMiner

Simple game in C
C++
3
star
24

my-terminal

πŸ’  Theme, Font, iterm2 preferences, zsh & plugins and my other terminal stuff
3
star
25

OpenGL

Great collection of useful utility programs in openGL(c++)
C++
2
star
26

countdown-begins

Free yourself and start doing what actually matters to you 😍
2
star
27

collabdraw

Collaborative drawing app in Node.js
2
star
28

proword

console and text mode based Wordpad Application developed in C
C++
2
star
29

fOS-Calculator

Calculator for firefox OS
JavaScript
2
star
30

experiment

Play on!
1
star
31

softvar

Hello, there!
1
star
32

ld-test

JavaScript
1
star
33

old-softvar.github.io

my website cum blog
CSS
1
star
34

System_Networking_Programming

Different networking programs written in python language.
Shell
1
star
35

site-engine

driver for my website cum blog
CSS
1
star
36

test-codesandbox

Testing codesandbox...
JavaScript
1
star
37

Melange

Design room for static webpage designing
PHP
1
star
38

test-diaglog

HTML
1
star
39

embed-jsfiddle

Web Component wrapper for jsFiddle's widgets using Polymer
JavaScript
1
star