Reputation: 49
I am trying to display JSON data(I received from S3 bucket) into a HTML table like structure but I am unable to figure out a way to do that.
import boto3
import json
import click
import requests
boto3.setup_default_session(profile_name='rli-dev', region_name='us-west-2')
s3 = boto3.resource('s3')
content_object = s3.Object('bsdev-data-validation', 'awsnightlyendtoend_bsdev_2018-10-24T11:53:45Z/validate_adwactivityfact/job-details.json')
file_content = content_object.get()['Body'].read().decode('utf-8-sig')
json_content = json.loads(file_content)
print(json_content)
I am expecting the JSON data that I have it in "json_content" into a HTML(table) like structure
Upvotes: 1
Views: 5721
Reputation: 557
Use json2html package:
$ pip install json2html
import boto3
import json
import click
import requests
from json2html import *
boto3.setup_default_session(profile_name='rli-dev', region_name='us-west-2')
s3 = boto3.resource('s3')
content_object = s3.Object('bsdev-data-validation', 'awsnightlyendtoend_bsdev_2018-
10-24T11:53:45Z/validate_adwactivityfact/job-details.json')
file_content = content_object.get()['Body'].read().decode('utf-8-sig')
json_content = json.loads(file_content)
print(json_content)
#Modify this to fit your need
input = {
"name": "json2html",
"description": "Converts JSON to HTML tabular representation"
}
test = json2html.convert(json = input)
print(test)
Output of print(test) is:
<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>
You can even do lists of JSON statements:
from json2html import *
input = [{
"name": "John Doe",
"description": "Generic Man",
"some data": "['brown hair', '5.10, 'accountant']"
}, {
"name": "Jane Doe",
"description": "Generic woman",
"some data": "['brown hair', '5.5, 'hair dresser']"
}]
test = json2html.convert(json = input)
print(test)
Output of list-JSON:
<table border="1"><thead><tr><th>name</th><th>description</th><th>some data</th></tr></thead><tbody><tr><td>John Doe</td><td>Generic
Man</td><td>['brown hair', '5.10, 'accountant']</td></tr><tr><td>Jane Doe</td><td>Generic woman</td><td>['brown hair', '5.5, 'hair dresser']</td></tr></tbody></table>
Upvotes: 2