There are a few Python libraries that have a special place in my heart. Like when I first found out about the XLSX writer library that lets you produce well-formatted Excel spreadsheets with highlighting and all that – I felt bad for how many programs I’d made over the years that just spit out CSV files and left it to the person using it to make them look pretty.
Back when I was with the government, we often had a fast “op-tempo” or “operation tempo”—basically meaning sometimes someone would need something and they’d need it right now. They’d come to my desk asking for something and wouldn’t leave until they had what they wanted. This was before AI, so there were times I’d write some quick Python to pull data from a JSON information feed and need to hand it over immediately.
The problem was, if I just gave them the raw JSON – even though you can open it up in notepad – it’s really ugly to look at and can be hard to figure out what’s going on. That’s when I found out about this Python library called JSON2HTML. It does exactly what it says in the name – takes JSON and saves it down into an HTML file, trying to do a decent job of making it look pretty.
Now, I’m not saying it’s going to create the most well-formatted, best-looking reports you’ve ever seen. But it does a good job of taking JSON and putting it into a format where you can just open it up in a browser and actually view it properly. If you’ve got some JSON that you need to share with other people, or even just look at yourself, and you don’t want to go through the trouble of formatting it for a nicer-looking report, definitely check out the JSON2HTML Python library.
Quick heads up – you’ll need to do a pip install before you can use it, as it’s not something that’s built into standard Python by default.
# First, we need to import a tool (called a "library") that helps us get data from the internet
# The 'requests' library makes it easy to fetch data from websites and APIs
import requests
# We also need the json library to help us display the data nicely
import json
# Import json2html to convert JSON to HTML
from json2html import *
# We use requests.get() to fetch data from a website
# This time we'll use the /facts endpoint and request 12 facts using the limit parameter
response = requests.get('https://catfact.ninja/facts?limit=12')
# The website sends back data in a format called JSON
fact_data = response.json()
# First let's see what the raw JSON data looks like
print('
Here is the raw JSON data from the API:')
print(json.dumps(fact_data, indent=2))
# Now fact_data contains a 'data' key with a list of facts
print('\n
Here are your random cat facts:')
# Loop through each fact in the data list and print it with a bullet point
for fact in fact_data['data']:
print(f'• {fact["fact"]}')
# Convert the JSON data to HTML and save it to a file
html_content = json2html.convert(json=fact_data)
# Add some basic CSS styling to make it look better
styled_html = f"""
<html>
<head>
<style>
body {{
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f0f0f0;
}}
table {{
border-collapse: collapse;
width: 100%;
background-color: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}}
th, td {{
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}}
th {{
background-color: #4CAF50;
color: white;
}}
h1 {{
color: #333;
text-align: center;
}}
</style>
</head>
<body>
<h1>
Random Cat Facts
</h1>
{html_content}
</body>
</html>
"""
# Save the HTML to a file
with open('cat_facts.html', 'w', encoding='utf-8') as f:
f.write(styled_html)
print('\n
HTML file has been created as "cat_facts.html"')