Compare JSON documents

Compare JSON documents in Python

GroupDocs.Comparison for Python via .NET allows developers to easily compare JSON files and detect differences in deeply nested objects, arrays, data types, and structures.
This is useful for validating APIs, configurations, and large datasets where accuracy is critical.

With the API, you can:

  • Compare two or more JSON documents\
  • Highlight differences in nested objects, arrays, and values\
  • Use visual mode (HTML report with highlights) or textual mode (JSON diff output)\
  • Apply or reject detected changes programmatically

Example: Compare JSON files in Python

import groupdocs.comparison as gc

source_path = "source.json"
target_path = "target.json"
result_path = "result.json"

# Initialize the comparer with the source JSON document
with gc.Comparer(source_path) as comparer:

    # Add the target JSON document
    comparer.add(target_path)

    # Compare and save the result file
    comparer.compare(result_path)

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

Visual comparison mode

Generate a single HTML report that highlights changes visually:\

  • Deleted items appear in red\
  • Inserted or modified items appear in blue
import groupdocs.comparison as gc
from groupdocs.comparison.options import CompareOptions

source_path = "source.json"
target_path = "target.json"
result_path = "result.html"

options = CompareOptions()
options.generate_summary_page = True  # Include summary of changes

with gc.Comparer(source_path) as comparer:
    comparer.add(target_path)
    comparer.compare(result_path, options)

Textual comparison mode

Produce a merged JSON file with inline diff markers:\

  • Deleted content is wrapped in [ ]\
  • Inserted content is wrapped in ( )
import groupdocs.comparison as gc
from groupdocs.comparison.options import CompareOptions

source_path = "source.json"
target_path = "target.json"
result_path = "result.json"

options = CompareOptions()
options.detect_style_changes = True
options.show_deleted_content = True

with gc.Comparer(source_path) as comparer:
    comparer.add(target_path)
    comparer.compare(result_path, options)

Applying or rejecting changes

You can also programmatically accept or reject detected changes in JSON comparison results:

import groupdocs.comparison as gc

source_path = "source.json"
target_path = "target.json"
result_path = "result.json"

with gc.Comparer(source_path) as comparer:
    comparer.add(target_path)

    # Run comparison first
    comparer.compare(result_path)

    # Iterate over detected changes
    for change in comparer.get_changes():
        if change.comparison_action == "Deleted":
            change.comparison_action = "Reject"  # reject deletion
        else:
            change.comparison_action = "Accept"  # accept additions/updates

    # Apply the updated decisions
    comparer.apply_changes(result_path)
Close
Loading

Analyzing your prompt, please hold on...

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