JSON Visualizer Script in Python.
About the project: This is a project for a JSON Visualizer. This project uses a graphical user interface (GUI) built with the standard tkinter library, allowing you to easily paste or type JSON data and see it formatted in a clean, readable way.
This script uses Python's built-in json module to parse and re-format the data, so you won't need to install any external libraries.
This project gives you a solid foundation for a JSON visualizer. It handles the core task of formatting valid JSON and provides clear feedback for invalid syntax.
Project Level: Intermediate
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# json_visualizer.py
import tkinter as tk
from tkinter import messagebox
import json
class JSONVisualizerApp:
"""
A simple GUI application to visualize and pretty-print JSON data.
"""
def __init__(self, root):
"""
Initializes the application window and widgets.
Args:
root (tk.Tk): The main Tkinter window.
"""
self.root = root
self.root.title("JSON Visualizer")
self.root.geometry("800x600")
self.root.configure(bg="#f0f0f0")
self.create_widgets()
def create_widgets(self):
"""
Builds and places all the GUI components.
"""
# Main Frame for padding and structure
main_frame = tk.Frame(self.root, padx=10, pady=10, bg="#f0f0f0")
main_frame.pack(expand=True, fill="both")
# --- Input Section ---
input_label = tk.Label(main_frame, text="Paste JSON here:", font=("Helvetica", 12, "bold"), bg="#f0f0f0")
input_label.pack(anchor="w", pady=(0, 5))
# Input Text Widget
self.json_input = tk.Text(main_frame, height=15, wrap="word", font=("Courier", 10), bg="#ffffff", relief="sunken", borderwidth=1)
self.json_input.pack(fill="x", pady=(0, 10))
# --- Button Section ---
button_frame = tk.Frame(main_frame, bg="#f0f0f0")
button_frame.pack(pady=5)
visualize_button = tk.Button(button_frame, text="Visualize JSON", command=self.visualize_json, font=("Helvetica", 12), bg="#4caf50", fg="white", relief="raised")
visualize_button.pack(side="left", padx=5)
clear_button = tk.Button(button_frame, text="Clear", command=self.clear_fields, font=("Helvetica", 12), bg="#f44336", fg="white", relief="raised")
clear_button.pack(side="left", padx=5)
# --- Output Section ---
output_label = tk.Label(main_frame, text="Formatted JSON:", font=("Helvetica", 12, "bold"), bg="#f0f0f0")
output_label.pack(anchor="w", pady=(10, 5))
# Output Text Widget
self.json_output = tk.Text(main_frame, height=15, wrap="word", font=("Courier", 10), bg="#e8e8e8", relief="sunken", borderwidth=1)
self.json_output.pack(expand=True, fill="both")
self.json_output.config(state="disabled") # Make output read-only
def visualize_json(self):
"""
Attempts to parse and pretty-print the JSON from the input field.
"""
json_string = self.json_input.get("1.0", tk.END).strip()
if not json_string:
messagebox.showwarning("Warning", "Please enter some JSON data to visualize.")
return
# Enable output widget for editing
self.json_output.config(state="normal")
self.json_output.delete("1.0", tk.END)
try:
# Parse the JSON string into a Python object
parsed_json = json.loads(json_string)
# Pretty-print the Python object back to a string with indentation
formatted_json = json.dumps(parsed_json, indent=4)
# Insert the formatted JSON into the output widget
self.json_output.insert("1.0", formatted_json)
except json.JSONDecodeError as e:
# Handle invalid JSON input
error_message = f"Invalid JSON syntax:\n{e}"
self.json_output.insert("1.0", error_message)
messagebox.showerror("Error", error_message)
# Disable output widget to make it read-only again
self.json_output.config(state="disabled")
def clear_fields(self):
"""
Clears the content of both the input and output fields.
"""
self.json_input.delete("1.0", tk.END)
self.json_output.config(state="normal")
self.json_output.delete("1.0", tk.END)
self.json_output.config(state="disabled")
def main():
"""Main function to start the application."""
root = tk.Tk()
app = JSONVisualizerApp(root)
root.mainloop()
if __name__ == "__main__":
main()
← Back to Projects