JSON Merge (Python)

1.9.2 · active · verified Sat Apr 11

jsonmerge is a Python library designed for merging a series of JSON documents into a single document. It is particularly useful for consolidating contributions from different authors to a common document or managing consecutive versions where fields are updated over time. The library leverages JSON Schema to define and apply various merge strategies for different parts of the document. The current version is 1.9.2, with an infrequent release cadence (e.g., updates in 2017 and 2023).

Warnings

Install

Imports

Quickstart

Demonstrates both the simple `merge` function for default behavior and the `Merger` class for schema-driven merging, specifically using the 'append' strategy for arrays.

import json
from jsonmerge import merge, Merger

# Basic merge without a schema (uses default objectMerge and overwrite strategies)
base = {"name": "John", "details": {"age": 30, "city": "New York"}, "tags": ["active", "premium"]}
head = {"details": {"age": 31, "country": "USA"}, "occupation": "Engineer", "tags": ["vip"]}

result_basic = merge(base, head)
print(f"Basic merge: {json.dumps(result_basic, indent=2)}")

# Merge with a schema to define specific strategies, e.g., 'append' for arrays
schema = {
    "properties": {
        "tags": {
            "mergeStrategy": "append"
        }
    }
}

merger = Merger(schema)
result_schema = merger.merge(base, head)
print(f"\nSchema-driven merge (append tags): {json.dumps(result_schema, indent=2)}")

view raw JSON →