A Python GUI Tool for JSON Formatting and Comparison
This article introduces a Python‑based GUI application that formats, compares, and highlights differences between two JSON files, detailing its features, implementation using tkinter, json and DeepDiff, example usage, and instructions for running and extending the tool.
In daily development, mismatched API responses, version changes, and environment configuration differences often stem from JSON data discrepancies; manually comparing JSON is inefficient and error‑prone, prompting the creation of a Python GUI tool that formats JSON and highlights differences.
Key features include JSON pretty‑printing, deep comparison of nested structures, green highlighting for added items, red for deletions, keyword search with yellow highlights, and a simple, intuitive interface suitable for beginners.
The tool is built with tkinter for the GUI, the built‑in json module for parsing and formatting, and DeepDiff for recursive comparison, handling value changes, type changes, added or removed keys, and list item modifications.
An example comparison shows how differences such as changed names, removed "gender" keys, added "age" keys, and new list items are detected and displayed with clear path information and color coding.
<code>#示例代码</code><code>import tkinter as tk</code><code>from tkinter import ttk, messagebox</code><code>import json</code><code>from deepdiff import DeepDiff</code><code>def format_json(json_str, widget):</code><code> try:</code><code> parsed = json.loads(json_str)</code><code> formatted = json.dumps(parsed, indent=4, ensure_ascii=False)</code><code> widget.delete(1.0, tk.END)</code><code> widget.insert(tk.END, formatted)</code><code> return parsed</code><code> except json.JSONDecodeError as e:</code><code> messagebox.showerror("错误", f"JSON 格式错误:\n{e}")</code><code> return None</code><code>def format_json1():</code><code> json1 = text_input1.get("1.0", tk.END)</code><code> format_json(json1, text_output1)</code><code>def format_json2():</code><code> json2 = text_input2.get("1.0", tk.END)</code><code> format_json(json2, text_output2)</code><code>def compare_json():</code><code> json1 = text_input1.get("1.0", tk.END)</code><code> json2 = text_input2.get("1.0", tk.END)</code><code> dict1 = format_json(json1, text_output1)</code><code> dict2 = format_json(json2, text_output2)</code><code> if dict1 is None or dict2 is None:</code><code> return</code><code> diff = DeepDiff(dict1, dict2, ignore_order=True)</code><code> text_diff.tag_configure("added", foreground="green")</code><code> text_diff.tag_configure("removed", foreground="red")</code><code> text_diff.delete(1.0, tk.END)</code><code> if not diff:</code><code> text_diff.insert(tk.END, "两个 JSON 完全一致")</code><code> else:</code><code> for key, changes in diff.items():</code><code> # handling of each diff type omitted for brevity</code><code>def highlight_line(text_diff, start_idx, old_val, new_val, tag1, tag2):</code><code> # implementation omitted</code><code>def search_in_diff():</code><code> # implementation omitted</code><code># GUI layout code omitted for brevity</code><code>root.mainloop()</code>To run the tool, install the DeepDiff library with pip install deepdiff , save the script as json_diff_tool.py , and execute python json_diff_tool.py to launch the graphical interface.
Future enhancements may include drag‑and‑drop file upload, exporting reports to HTML or PDF, packaging as a standalone executable, and supporting additional formats such as YAML or XML.
In summary, this lightweight yet powerful utility streamlines JSON comparison tasks for front‑end/back‑end integration, automated testing, and configuration management, significantly improving productivity.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.