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).
pip install jsonmergeVerified import paths — ran on the pinned version, not inferred.
Demonstrates both the simple `merge` function for default behavior and the `Merger` class for schema-driven merging, specifically using the 'append' strategy for arrays.
For schemas based on newer drafts, initialize `Merger` with `Merger(schema, validatorclass=jsonschema.Draft7Validator)` (or the appropriate validator class for your draft version).
Always validate your `base` and `head` JSON documents against your merge schema using `jsonschema.validate()` *before* performing the merge operation if strict validation is required.
Ensure your project runs on Python 3.5 or a more recent version.
Define an explicit `mergeStrategy` keyword for schema elements that use `allOf`, `anyOf`, or `oneOf` to guide the merger's behavior in complex schema structures.
Refer to the latest documentation for alternatives or updated usage if you rely on adding metadata with the `version` strategy.
Install the `jsonmerge` library using pip: `pip install jsonmerge`
Thoroughly review your JSON schema, ensuring all relevant parts of your document have appropriate `mergeStrategy` keywords defined. For complex schema keywords like `allOf`, `anyOf`, and `oneOf`, explicitly define a merge strategy. Crucially, validate your `base` and `head` JSON documents against your schema using a dedicated validation library like `jsonschema` *before* calling `jsonmerge.merge()` to catch structural or type mismatches early.
Inspect the types of data in your `base` and `head` documents at the location where the error occurs. Ensure that the merge strategy defined (or implied by default) in your schema is appropriate for the data types being merged. If merging different types is a requirement (e.g., replacing a string with an object), you may need to define a custom merge strategy or pre-process your data to ensure type compatibility before merging.