Compare JSON Documents

GroupDocs.Comparison for Python via .NET compares JSON files and detects differences in nested objects, arrays, data types, and structures. This is useful for validating APIs, configurations, and large datasets where accuracy is critical.

The API can:

  • Compare two or more JSON documents.
  • Highlight differences in nested objects, arrays, and values.
  • Produce a visual HTML report with highlighted differences, or a textual merged JSON file with inline diff markers.
  • Accept or reject detected changes programmatically.

Example 1: Compare JSON files

from groupdocs.comparison import Comparer

def compare_json_documents():
    with Comparer("./source.json") as comparer:
        comparer.add("./target.json")
        comparer.compare("./result.json")

if __name__ == "__main__":
    compare_json_documents()

source.json is the source file used in this example. Click here to download it.

target.json is the target file used in this example. Click here to download it.

result.html (635 bytes)
result.json (170 bytes)

Download full output

The resulting result.json contains the combined JSON structure with differences highlighted inline.

Example 2: Visual comparison (HTML report with summary)

Generate a single HTML report that highlights changes visually — deletions in red, insertions or modifications in blue. Add a summary page that lists every change.

from groupdocs.comparison import Comparer
from groupdocs.comparison.options import CompareOptions

def compare_json_to_html():
    options = CompareOptions()
    options.generate_summary_page = True

    with Comparer("./source.json") as comparer:
        comparer.add("./target.json")
        comparer.compare("./result.html", options)

if __name__ == "__main__":
    compare_json_to_html()

source.json is the source file used in this example. Click here to download it.

target.json is the target file used in this example. Click here to download it.

Example 3: Textual comparison with style detection

Produce a merged JSON file with inline diff markers; detect style and deletion changes.

from groupdocs.comparison import Comparer
from groupdocs.comparison.options import CompareOptions

def compare_json_textual():
    options = CompareOptions()
    options.detect_style_changes = True
    options.show_deleted_content = True

    with Comparer("./source.json") as comparer:
        comparer.add("./target.json")
        comparer.compare("./result.json", options)

if __name__ == "__main__":
    compare_json_textual()

source.json is the source file used in this example. Click here to download it.

result.html (635 bytes)
result.json (170 bytes)

Download full output

When to use each output mode

Result extensionOutputBest for
.jsonMerged JSON with inline diff markers ([deleted], (inserted))Programmatic diff post-processing
.htmlSingle HTML report with deletions in red and insertions in blueHuman review, embedded into a PR-review UI
.md / .txtPlain-text diff representationCI log output, ChatOps notifications

generate_summary_page = True appends a high-level change-count summary to any output format. detect_style_changes = True ensures structural / type-level changes in JSON (e.g., a string becoming a number) are surfaced explicitly.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.