json2html


Namejson2html JSON
Version 1.3.0 PyPI version JSON
download
home_pagehttps://github.com/softvar/json2html
SummaryJSON to HTML Table Representation
upload_time2019-07-03 20:50:03
maintainer
docs_urlNone
authorVarun Malhotra
requires_python
licenseMIT
keywords json html table
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            json2html
=========

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

|Latest Version| |Downloads| |Build|

.. |Build| image:: https://api.travis-ci.org/softvar/json2html.png

.. |Latest Version| image:: https://img.shields.io/pypi/v/json2html.svg
    :target: https://pypi.python.org/pypi/json2html

.. |Downloads| image:: https://img.shields.io/pypi/dm/json2html.svg
        :target: https://pypi.python.org/pypi/json2html

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.

.. code-block:: bash

	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 <http://json2html.varunmalhotra.xyz/>`_ 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
------------

.. code-block:: bash

	$ 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

.. code-block:: python

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

Output:

.. code-block:: bash

	<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

.. code-block:: python

	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:

.. code-block:: bash

	<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

.. code-block:: python

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

Output:

.. code-block:: bash

	<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

.. code-block:: python

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

Output:

.. code-block:: bash

	<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 <http://json.org/example>`_]

.. code-block:: python

	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:

.. code-block:: bash

	<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
------

.. code-block:: bash

	cd test/
	python run_tests.py

Tested with Python 2.6, 2.7 3.4, 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 <https://opensource.org/licenses/MIT>`_

	Copyright (c) 2014-2017 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.
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/softvar/json2html",
    "name": "json2html",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "json,HTML,Table",
    "author": "Varun Malhotra",
    "author_email": "varun2902@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/01/d5/40b617ee19d2d79f606ed37f8a81e51158f126d2af67270c68f2b47ae0d5/json2html-1.3.0.tar.gz",
    "platform": "",
    "description": "json2html\n=========\n\nPython wrapper to convert ``JSON`` into a human readable ``HTML Table`` representation.\n\n|Latest Version| |Downloads| |Build|\n\n.. |Build| image:: https://api.travis-ci.org/softvar/json2html.png\n\n.. |Latest Version| image:: https://img.shields.io/pypi/v/json2html.svg\n    :target: https://pypi.python.org/pypi/json2html\n\n.. |Downloads| image:: https://img.shields.io/pypi/dm/json2html.svg\n        :target: https://pypi.python.org/pypi/json2html\n\nFeatures\n--------\n\n* User friendly tablular fomat, easy to read and share.\n* 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.\n\n.. code-block:: bash\n\n\tinput = {\n\t\t\"sampleData\": [{\n\t\t\t\"a\":1, \"b\":2, \"c\":3\n\t\t}, {\n\t\t\t\"a\":5, \"b\":6, \"c\":7\n\t\t}]\n\t}\n\n\twill create only one row combining the results. This feature can be turned off by explicitly passing an argument ``clubbing = False``.\n\n* Generated table can be provided some ``attributes`` explicitly. Eg. giving an ``id``, ``class`` or any ``data-*`` attribute.\n* Python 3 compatible\n\nLive Demo\n----------\n\n`Click here <http://json2html.varunmalhotra.xyz/>`_ for the online demo.\n\nList of valid arguments\n-----------------------\n\n``json2html.convert`` - The module's ``convert`` method accepts the following arguments:\n\n===================== ================\nArgument              Description\n--------------------- ----------------\n`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.\n--------------------- ----------------\n`table_attributes`    e.g. pass `id=\"info-table\"` or `class=\"bootstrap-class\"`/`data-*` to apply these attributes to the generated table\n--------------------- ----------------\n`clubbing`            turn on[default]/off clubbing of list with same keys of a dict / Array of objects with same key\n--------------------- ----------------\n`encode`              turn on/off[default] encoding of result to escaped html, compatible with any browser\n--------------------- ----------------\n`escape`              turn on[default]/off escaping of html tags in text nodes (prevents XSS attacks in case you pass untrusted data to json2html)\n===================== ================\n\nInstallation\n------------\n\n.. code-block:: bash\n\n\t$ pip install json2html\n\nOr, Download [here](https://github.com/softvar/json2html/releases) and run `python setup.py install` after changing directory to `/json2html`\n\nExample Usage\n-------------\n\n**Example 1:** Basic usage\n\n.. code-block:: python\n\n\tfrom json2html import *\n\tinput = {\n\t\t\"name\": \"json2html\",\n\t\t\"description\": \"Converts JSON to HTML tabular representation\"\n\t}\n\tjson2html.convert(json = input)\n\nOutput:\n\n.. code-block:: bash\n\n\t<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>\n\n============ ========================================================\nname         json2html\n------------ --------------------------------------------------------\ndescription  Converts JSON to HTML tabular representation\n============ ========================================================\n\n**Example 2:** Setting custom attributes to table\n\n.. code-block:: python\n\n\tfrom json2html import *\n\tinput = {\n\t\t\"name\": \"json2html\",\n\t\t\"description\": \"Converts JSON to HTML tabular representation\"\n\t}\n\tjson2html.convert(json = input, table_attributes=\"id=\\\"info-table\\\" class=\\\"table table-bordered table-hover\\\"\")\n\nOutput:\n\n.. code-block:: bash\n\n\t<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>\n\n**Example 3:** Clubbing same keys of: Array of Objects\n\n.. code-block:: python\n\n\tfrom json2html import *\n\tinput = {\n\t\t\"sample\": [{\n\t\t\t\"a\":1, \"b\":2, \"c\":3\n\t\t}, {\n\t\t\t\"a\":5, \"b\":6, \"c\":7\n\t\t}]\n\t}\n\tjson2html.convert(json = input)\n\nOutput:\n\n.. code-block:: bash\n\n\t<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>\n\n======== ======= =======\n  a         c      b\n-------- ------- -------\n   1        3       2\n-------- ------- -------\n   5        7       6\n======== ======= =======\n\n**Example 4:** Each row for different key(s) of: Array of Objects\n\n.. code-block:: python\n\n\tfrom json2html import *\n\tinput = {\n\t\t\"sample\": [{\n\t\t\t\"a\":1, \"b\":2, \"c\":3\n\t\t}, {\n\t\t\t\"1a1\":5, \"1b1\":6, \"c\":7\n\t\t}]\n\t}\n\tjson2html.convert(json = input)\n\nOutput:\n\n.. code-block:: bash\n\n\t<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>\n\n**Example 5:** [Source: `json.org/example <http://json.org/example>`_]\n\n.. code-block:: python\n\n\tfrom json2html import *\n\n\tinput = {\n\t\t\"glossary\": {\n\t\t\t\"title\": \"example glossary\",\n\t\t\t\"GlossDiv\": {\n\t\t\t\t\"title\": \"S\",\n\t\t\t\t\"GlossList\": {\n\t\t\t\t\t\"GlossEntry\": {\n\t\t\t\t\t\t\"ID\": \"SGML\",\n\t\t\t\t\t\t\"SortAs\": \"SGML\",\n\t\t\t\t\t\t\"GlossTerm\": \"Standard Generalized Markup Language\",\n\t\t\t\t\t\t\"Acronym\": \"SGML\",\n\t\t\t\t\t\t\"Abbrev\": \"ISO 8879:1986\",\n\t\t\t\t\t\t\"GlossDef\": {\n\t\t\t\t\t\t\t\"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\n\t\t\t\t\t\t\t\"GlossSeeAlso\": [\"GML\", \"XML\"]\n\t\t\t\t\t\t},\n\t\t\t\t\t\t\"GlossSee\": \"markup\"\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tjson2html.convert(json = input)\n\nOutput:\n\n.. code-block:: bash\n\n\t<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>\n\nTests\n------\n\n.. code-block:: bash\n\n\tcd test/\n\tpython run_tests.py\n\nTested with Python 2.6, 2.7 3.4, and 3.5.\n\nContributors\n------------\n\n1. Michel Mueller: [@muellermichel](https://github.com/muellermichel)\n\t* Added support for clubbing Array of Objects with same keys, more readable format.\n\t* Added support for adding custom `table_attributes`.\n\t* Convert now accepts unicode and bytestrings for the keyword argument \"json\".\n\t* Output now should always appear in the same order as input.\n\t* Now supports JSON Lists (at top level), including clubbing.\n\t* Now supports empty inputs and positional arguments for convert.\n\t* Python 3 support ; Added integration tests for Python 2.6, 3.4 and 3.5 such that support doesn't break.\n\t* Can now also do the proper encoding for you (disabled by default to not break backwards compatibility).\n\t* Can now handle non-JSON objects on a best-effort principle.\n\t* Now by default escapes html in text nodes to prevent XSS attacks.\n\n2. Daniel Lekic: [@lekic](https://github.com/lekic)\n\t* Fixed issue with one-item lists not rendering correctly.\n\t* General code cleanup, fixed all naming conventions and coding standards to adhere to PEP8 conventions.\n\n3. Kyle Smith: [@smithk86](https://github.com/smithk86)\n    * Added thead and tbody tags to group header and content rows when creating a table from an array of objects.\n\nCopyright and License\n---------------------\n\n\tThe `MIT license <https://opensource.org/licenses/MIT>`_\n\n\tCopyright (c) 2014-2017 Varun Malhotra\n\n\tPermission 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:\n\n\tThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\n\tTHE 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.",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "JSON to HTML Table Representation",
    "version": "1.3.0",
    "split_keywords": [
        "json",
        "html",
        "table"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "4353f09bf984c46eccd2665017b5eca0",
                "sha256": "8951a53662ae9cfd812685facdba693fc950ffc1c1fd1a8a2d3cf4c34600689c"
            },
            "downloads": -1,
            "filename": "json2html-1.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4353f09bf984c46eccd2665017b5eca0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6977,
            "upload_time": "2019-07-03T20:50:03",
            "upload_time_iso_8601": "2019-07-03T20:50:03.023972Z",
            "url": "https://files.pythonhosted.org/packages/01/d5/40b617ee19d2d79f606ed37f8a81e51158f126d2af67270c68f2b47ae0d5/json2html-1.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2019-07-03 20:50:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "softvar",
    "github_project": "json2html",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "json2html"
}
        
Elapsed time: 0.01887s