11

I've been using the JSON library for Python to get data from JSON files using Python.

infoFromJson = json.loads(jsonfile)

I fully understand how to work with JSON files in Python. However, I am trying to find a way to format JSON format in a nice way.

I prefer to convert the JSON into a nested HTML table format.

I found json2html for Python, which does exactly what I just described. However, it does not actually output anything when I run the script they provide.

Has anyone had experience with this tool? Or does anyone have suggestions for alternatives?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
OMGitzMidgar
  • 689
  • 2
  • 10
  • 28

2 Answers2

21

Try the following:

infoFromJson = json.loads(jsonfile)
print(json2html.convert(json = infoFromJson)) 

The result from json2html.convert is a string.

If you don't have module:

$ pip install json2html

More examples here.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Kyle Shrader
  • 912
  • 11
  • 16
  • This works, thank you! For some reason, if I try to combine the two lines you provided into one line, it does not print out anything (including no errors). Thanks for the help! – OMGitzMidgar Jun 23 '15 at 19:38
  • I doubt it's a naming/scope issue, but does it work in a single line if you alias the import for json to a different name? – Kyle Shrader Jun 23 '15 at 21:14
  • Sorry, can you further explain what you mean by "aliasing the import for json to a different name" ? – OMGitzMidgar Jun 23 '15 at 22:05
  • The issue you're seeing while doing this in one line might be the fact that the _json_ library and the _json_ default parameter are named similarly. But i have no idea :P `import json as jason; print json2html.convert(json = jason.loads(jsonfile)` none the less, glad it works. ^_^ – Kyle Shrader Jun 23 '15 at 22:27
  • 2
    At least for me, it looks like `import json2html` and `json2html.json2html.convert` – Robert Lugg Aug 14 '19 at 02:18
  • Is it possible to make some fields editable by changing the `table_attributes` in `son2html.convert()` function? – SBDK8219 Mar 30 '20 at 17:40
  • It is recommended to use the following way to import: `from json2html import *` – Constantine Kurbatov Oct 16 '21 at 20:56
3

Nowadays it's better to use json2table (at least for Python 3)

import json2table
import json

infoFromJson = json.loads(jsonfile)
build_direction = "LEFT_TO_RIGHT"
table_attributes = {"style": "width:100%"}
print(json2table.convert(infoFromJson, 
                         build_direction=build_direction, 
                         table_attributes=table_attributes))
Vedanta6
  • 99
  • 1
  • 5
  • 5
    I tried out both json2html and json2table with python 3 and had some issues with json2table using complex json structures. I am going with json2html and can't see why I shouldn't. Code looks ok and project is maintained. – mit Aug 07 '19 at 16:38
  • This version is good as has more flexibility in formatting. – Constantine Kurbatov Oct 16 '21 at 21:03
  • Only problem with json2html is that after you convert complex JSON into html, the final format is not readable by python FPDF. – shailesh gavathe Mar 07 '23 at 17:44