softvar / json2html Goto Github PK
View Code? Open in Web Editor NEW๐ Python module for converting complex JSON to HTML Table representation
Home Page: https://pypi.python.org/pypi/json2html
License: MIT License
๐ Python module for converting complex JSON to HTML Table representation
Home Page: https://pypi.python.org/pypi/json2html
License: MIT License
The function doesn't provide favorable results on dynamically created json (values collected as json dump from mongodb)
But if the same json is printed in console and the static output is used in code it works exactly as it should.
This doesn't work
response = db.reports.find({"MobileNo":No}) // mongo query returns cursor
aggregate = dumps(response) // dumps json
final['sample'] = aggregate
table = json2html.convert(json = final)
But if you copy the console output of aggregate and pass it a static string to the json2html.convert() it works perfectly
eg print aggregate
[{"eosinophils": "03", "mchc": "336", "hct": "333", "mch": "190", "_id": {"$oid": "567c385ac3ea3256121cb6f5"}, "rbc": "563"}, {"eosinophils": "03", "mchc": "336", "hct": "333", "mch": "190", "_id": {"$oid": "567c3a5ac3ea3256cb1ff2a5"}, "rbc": "563"}]
response = db.reports.find({"MobileNo":No}) // mongo query returns cursor
aggregate = dumps(mongo_query_response)
final['sample'] = [{"eosinophils": "03", "mchc": "336", "hct": "333", "mch": "190", "_id": {"$oid": "567c385ac3ea3256121cb6f5"}, "rbc": "563"}, {"eosinophils": "03", "mchc": "336", "hct": "333", "mch": "190", "_id": {"$oid": "567c3a5ac3ea3256cb1ff2a5"}, "rbc": "563"}]
table = json2html.convert(json = final)
from json2html import *
input = {
"sample": [{
"a":1, "b":2, "c":3
}, {
"a":5, "b":6, "c":7
}]
}
json2html.convert(json = input)
above code will have 4 columns whereas it is mentioned in the example that only 3 columns created. please refer example 3.
I found this tool quite useful.
Only problem I have is it doesn't convert Boolean value, casting empty.
For example, I tried this json code in JSON Editor Online and it properly shows what I expected.
{
"Example": {
"HF": {
"before": true,
"after": false
}
}
}
However, when I used this tool, it doesn't show any Boolen value.
Hi,
I'v got an issue with json string like:
from json2html import *
aa = [
{"aaa": 1},
{"bbb": "ccc"}
]
html = json2html.convert(
json=aa
)
print html
said:
Exception: Not a valid JSON list
but online demo can handle it w/o problem and it's valid json.
What are your thoughts on adding identifiers (name/id) to the elements?
Is it useful to set:
Hi there,
I'm using your library to produce HTML tables to good effect in most cases.
However, when the dictionary input involves apostrophes, the output fails to play nice with Folium popups.
Using html.escape
seems to fix that.
For example, running
from json2html import json2html as jh
import html
d = {'stop_name': "Ponsonby Rd & K'Rd"}
dd = {}
for k, v in d.items():
try:
vv = html.escape(v)
except AttributeError:
vv = v
dd[k] = vv
jh.convert(d), jh.convert(dd)
produces
'<table border="1"><tr><th>stop_name</th><td>Ponsonby Rd & K\'Rd</td></tr></table>',
'<table border="1"><tr><th>stop_name</th><td>Ponsonby Rd &amp; K&#x27;Rd</td></tr></table>'
The first output crashes Folium popups, but the second works with them.
So would it be better for json2html to escape apostrophes in the same way that html.escape
does?
Just did a pip install json2html with no errors. However, when I did "from json2html import *" I got:
from jsonconv import *
ImportError: No module named 'jsonconv'
Where can I get that module to install?
I am running Python 3.4
Perhaps with a little hint I could do the conversion.
Thanks. :)
I've had this idea on how to extend the simplicity of json2html for both read and write operations on the client side.
How about we'd build a JS based on jquery that
(a) runs over a json2html generated table (passing in a jquery selector) and wraps all text nodes except those that have been generated from a json dict key in input text elements. So the whole table becomes a form.
(b) adds a way to add new rows / subtables by allowing to copy a whole json2html generated structure and replace the text content with empty strings. This could be a rightclick-action on the background of the selected structure or some button inserted in a good place.
(c) adds a submit button at the end.
(d) when clicking submit it parses the table and basically does the reverse operation (html2json), so the server can get back a json. The whole implementation should work such that if the user doesn't edit anything, the posted json should be exactly the same as the one that was used to generate the html in the first place.
Now this would be quite a project, but imagine how cool it would be if you could have a configuration editor in your browser for any kind of JSON file and you wouldn't have to lift a finger other than installing json2html on the server and adding this Javascript to the response. It would be trivial to provide that json2html tooling as a Flask plugin as well at that point.
Trying to install on a linux system without a configured unicode locale in python3 gives an error:
# pip3 install json2html
Collecting json2html
Downloading json2html-1.1.1.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-nm_jwbmx/json2html/setup.py", line 17, in <module>
long_description=open('README.rst').read(),
File "/usr/lib/python3.5/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6233: ordinal not in range(128)
A workaround is to actually set an appropriate locale, but this is not always possible or convenient.
Dear developers,
I want to create a Conda package for a package of mine, which has json2html as a dependency.
However, json2html currently does not have a Conda package.
Would it be possible for you to create and publish one?
Thank you very much!
Cheers
I'm not sure if this is just for python 2.6.6, but I ran into an issue when trying to use json2html.
When running a simple script that uses json2html's convert function, I received the following error:
File "/usr/lib/python2.6/site-packages/json2html/jsonconv.py", line 50, in convert
ordered_json = json.loads(self.json_input, object_pairs_hook=ordereddict.OrderedDict)
File "/usr/lib64/python2.6/json/init.py", line 318, in loads
return cls(encoding=encoding, **kw).decode(s)
TypeError: init() got an unexpected keyword argument 'object_pairs_hook'
So there seems to be an issue with the line ordered_json = json.loads(self.json_input, object_pairs_hook=ordereddict.OrderedDict)
I solved this issue on my machine by installing simplejson, and then changing the use of the json library to the simplejson library.
Would you say this is "1.0"? It works great. 0.3 will give the impression it isn't functional.
How can I only import "convert". Doing "from json2html import convert" does not work.
I have a situation where "import *" throws error "import * only allowed at module level".
I am not a Python expert, so please understand if there is an obvious solution.
Thanks
thanks for your project.
it's pretty good, I have a question: how to prevent XSS?
does this project provide this function?
thanks :-)
With the following definition,
class TaskType(str, enum.Enum):
TASK_TYPE1 = 'task_type1'
TASK_TYPE2 = 'task_type2'
the enum class instance is converted into a list. I would expect a string instead since str
mixin is being inherited.
In [78]: json2html.json2html.convert({'task_type': TaskType.TASK_TYPE1})
'<table border="1"><tr><th>task_type</th><td><ul><li>t</li><li>a</li><li>s</li><li>k</li><li>_</li><li>t</li><li>y</li><li>p</li><li>e</li><li>1</li></ul></td></tr></table>'
Thanks!
The last Python 2.6 Travis build has failed (which came from #41):
Downloading archive: https://storage.googleapis.com/travis-ci-language-archives/python/binaries/ubuntu/16.04/x86_64/python-2.6.tar.bz2
0.13s$ curl -sSf -o python-2.6.tar.bz2 ${archive_url}
curl: (22) The requested URL returned error: 404 Not Found
Unable to download 2.6 archive. The archive may not exist. Please consider a different version
https://travis-ci.org/softvar/json2html/jobs/583374664
Apparently there are some issues with Ubuntu and Python 2.6 in Travis, Python 2.6 will require the use of dist: trusty
(which I believe is itself EOLed).
However, do we still need/want to support Python 2.6? it was EOLed in 2013.
Hello,
Only Question
It is the best library for json to html ๐
I have one question:
I use this library for automation test (GUI) with selenium and python.
Is it possible that in the dictionary I have Value = Pass to be colored with Green and when Value = Fail to be colored with Red?
Example json input:
{
"Numar_Special_Achizitie_Mobile_Service_and_Device_6":{
"Status":"PASSED",
"HOST Name":"Windows OS",
"Release":"None Given, Hardcoded",
"EXECUTION_TS":"20200821124933",
"Tests Execution Log":{
"test_page_Login":{
"Status":"PASSED",
"ExecutionLog":{
"Click_OK":"PASSED",
"Type_UserName":"PASSED",
"Type_Password":"PASSED",
"Click_Login":"PASSED",
"Select by index_DealerSelect":"PASSED"
}
},
"test_page_Search":{
"Status":"FAIL",
"ExecutionLog":{
"Click_MSISDN":"PASSED",
"Type_searchKey_MSISDN":"PASSED",
"Click_cauta":"FAIL"
}
},
Thanks
This json is rendered incorrectly in v1.3.0
{ "key10": { "key20": { "a": "a1", "b": "b1", "key30": [1, 2, 3, 4] } } }
The key30 sub-table is displayed before the b/b1 name/value
key10 |
|
---|
Ironically, the online demo works correctly.
Hi,
My html output with Alphabetical order instead of my json output file.
Ex: json output is
data [
{
name: 'gopi'
Age: '12'
colour: 'blud'},
{
name: 'john'
Age: ''23'
colour: 'Red'
}
]
my Html output
Age | colour | Name
12 | blue| Gopi
23| red | john
but i need output as
Name| Age| colour
Can someone help me how to sort this ?
Regards
Gopi
Hi,
That is a useful module. Thanks.
Do you plan to make it compatible with python3?
Michael
Should we consider using style=
instead?
The default could be
<style>table, th, td {border: 1px solid black;}</style>
<table><tr><th>name</th><td>Json2Html</td></tr><tr><th>description</th><td>converts json 2 html table format</td></tr></table>
The problem here is that the generated <style>
might of course clash with the user's existing style. A longer but safer alternative would be to use inline styles for each <table>
, <th>
and <td>
tag:
<table style="border: 1px solid black;">
<tr>
<th style="border: 1px solid black;">name</th>
<td style="border: 1px solid black;">Json2Html</td>
</tr>
<tr>
<th style="border: 1px solid black;">description</th>
<td style="border: 1px solid black;">converts json 2 html table format</td>
</tr>
</table>
Sometimes inorder to represent the data properly, we'd want to generate the html top to bottom, i.e., in the fomat. This could be done using the build direction as a custom input.
-------------------------------------
Name | Age | Gender | DOB |
Rohith | 20 | Male | Jan |
-------------------------------------
as the title says. okay if you have decided to remove the demo
Should the documentation be updated for this?
from json2html import json2html
for Advanced table formatting, Is it a better way to colorful the value?
Passing an argument to turn it off. By default, i will remain on as it seems good to have.
When a key in the input json contains non-ascii character an UnicodeEncodeError exception is raised.
Proposed change in row 121:
[-] convertedOutput = convertedOutput + '<th>' + str(k) + '</th>'
[+] convertedOutput = convertedOutput + '<th>' + markup(k) + '</th>'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.