Skip to content

Commit 310e548

Browse files
committed
chore: convert README from rst to md format
1 parent e1feea2 commit 310e548

File tree

2 files changed

+216
-241
lines changed

2 files changed

+216
-241
lines changed

README.md

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
# json2html
2+
3+
Python module to convert `JSON` into a human readable `HTML Table` representation.
4+
5+
![Latest Version](https://img.shields.io/pypi/v/json2html.svg) ![Downloads](https://img.shields.io/pypi/dm/json2html.svg) [![CI](https://github.com/softvar/json2html/workflows/CI/badge.svg?branch=master)](https://github.com/softvar/json2html/actions?query=workflow%3ACI)
6+
7+
## Features
8+
9+
- User friendly tabular format, easy to read and share.
10+
- If the value of the key is an array of objects and all the keys are the same (value of the key is a dict of a list), the module will club by default. E.g.:
11+
12+
```bash
13+
input = {
14+
"sampleData": [{
15+
"a":1, "b":2, "c":3
16+
}, {
17+
"a":5, "b":6, "c":7
18+
}]
19+
}
20+
```
21+
22+
This will create only one row combining the results. This feature can be turned off by explicitly passing an argument `clubbing = False`.
23+
24+
- The generated table can have some `attributes` explicitly. E.g. giving an `id`, `class`, or any `data-*` attribute.
25+
- Python 3 compatible.
26+
27+
## Live Demo
28+
29+
[Click here](http://json2html.varunmalhotra.xyz/) for the online demo.
30+
31+
## List of Valid Arguments
32+
33+
`json2html.convert` - The module's `convert` method accepts the following arguments:
34+
35+
| Argument | Description |
36+
|---------------------|-------------|
37+
| `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. |
38+
| `table_attributes` | E.g. pass `id="info-table"` or `class="bootstrap-class"`/`data-*` to apply these attributes to the generated table. |
39+
| `clubbing` | Turn on [default]/off clubbing of list with the same keys of a dict / Array of objects with the same key. |
40+
| `encode` | Turn on/off [default] encoding of result to escaped HTML, compatible with any browser. |
41+
| `escape` | Turn on [default]/off escaping of HTML tags in text nodes (prevents XSS attacks in case you pass untrusted data to json2html). |
42+
43+
## Installation
44+
45+
```bash
46+
pip install json2html
47+
```
48+
49+
Or, Download [here](https://github.com/softvar/json2html/releases) and run `python setup.py install` after changing directory to `/json2html`
50+
51+
## Example Usage
52+
53+
**Example 1:** Basic usage
54+
55+
```python
56+
from json2html import *
57+
input = {
58+
"name": "json2html",
59+
"description": "Converts JSON to HTML tabular representation"
60+
}
61+
json2html.convert(json = input)
62+
```
63+
64+
Output:
65+
66+
```html
67+
<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>
68+
```
69+
70+
| name | description |
71+
|-------------|--------------------------------------------------|
72+
| json2html | Converts JSON to HTML tabular representation |
73+
74+
75+
**Example 2:** Setting custom attributes to table
76+
77+
```python
78+
from json2html import *
79+
input = {
80+
"name": "json2html",
81+
"description": "Converts JSON to HTML tabular representation"
82+
}
83+
json2html.convert(json = input, table_attributes="id=\"info-table\" class=\"table table-bordered table-hover\"")
84+
```
85+
86+
Output:
87+
88+
```html
89+
<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>
90+
```
91+
92+
**Example 3:** Clubbing same keys of: Array of Objects
93+
94+
```python
95+
from json2html import *
96+
input = {
97+
"sample": [{
98+
"a":1, "b":2, "c":3
99+
}, {
100+
"a":5, "b":6, "c":7
101+
}]
102+
}
103+
json2html.convert(json = input)
104+
```
105+
106+
Output:
107+
108+
```html
109+
<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>
110+
```
111+
112+
| a | c | b |
113+
|-----|-----|-----|
114+
| 1 | 3 | 2 |
115+
| 5 | 7 | 6 |
116+
117+
118+
**Example 4:** Each row for different key(s) of: Array of Objects
119+
120+
```python
121+
from json2html import *
122+
input = {
123+
"sample": [{
124+
"a":1, "b":2, "c":3
125+
}, {
126+
"1a1":5, "1b1":6, "c":7
127+
}]
128+
}
129+
json2html.convert(json = input)
130+
```
131+
132+
Output:
133+
134+
```html
135+
<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>
136+
```
137+
138+
**Example 5:** [Source: `json.org/example <http://json.org/example>`_]
139+
140+
```python
141+
from json2html import *
142+
143+
input = {
144+
"glossary": {
145+
"title": "example glossary",
146+
"GlossDiv": {
147+
"title": "S",
148+
"GlossList": {
149+
"GlossEntry": {
150+
"ID": "SGML",
151+
"SortAs": "SGML",
152+
"GlossTerm": "Standard Generalized Markup Language",
153+
"Acronym": "SGML",
154+
"Abbrev": "ISO 8879:1986",
155+
"GlossDef": {
156+
"para": "A meta-markup language, used to create markup languages such as DocBook.",
157+
"GlossSeeAlso": ["GML", "XML"]
158+
},
159+
"GlossSee": "markup"
160+
}
161+
}
162+
}
163+
}
164+
}
165+
166+
json2html.convert(json = input)
167+
```
168+
169+
Output:
170+
171+
```html
172+
<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>
173+
```
174+
175+
## Tests
176+
177+
```bash
178+
cd test/
179+
python run_tests.py
180+
```
181+
182+
Tested on Python 2.7 and 3.5+.
183+
184+
## Contributors
185+
186+
1. Michel Mueller: [@muellermichel](https://github.com/muellermichel)
187+
188+
* Added support for clubbing Array of Objects with same keys, more readable format.
189+
* Added support for adding custom `table_attributes`.
190+
* Convert now accepts unicode and bytestrings for the keyword argument "json".
191+
* Output now should always appear in the same order as input.
192+
* Now supports JSON Lists (at top level), including clubbing.
193+
* Now supports empty inputs and positional arguments for convert.
194+
* Python 3 support ; Added integration tests for Python 2.6, 3.4 and 3.5 such that support doesn't break.
195+
* Can now also do the proper encoding for you (disabled by default to not break backwards compatibility).
196+
* Can now handle non-JSON objects on a best-effort principle.
197+
* Now by default escapes html in text nodes to prevent XSS attacks.
198+
199+
2. Daniel Lekic: [@lekic](https://github.com/lekic)
200+
* Fixed issue with one-item lists not rendering correctly.
201+
* General code cleanup, fixed all naming conventions and coding standards to adhere to PEP8 conventions.
202+
203+
3. Kyle Smith: [@smithk86](https://github.com/smithk86)
204+
* Added thead and tbody tags to group header and content rows when creating a table from an array of objects.
205+
206+
## Copyright and License
207+
208+
The `MIT license <https://opensource.org/licenses/MIT>`_
209+
210+
Copyright (c) 2013-2024 Varun Malhotra
211+
212+
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:
213+
214+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
215+
216+
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.

0 commit comments

Comments
 (0)