简体   繁体   中英

Converting JSON to HTML table in Python

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?

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM