A BSON implementation of RFC 6902 to compute the difference between two BSON documents
Switch branches/tags
Clone or download


This is an implementation of RFC 6902 JSON Patch written in Java.

This JSON Patch implementation works directly with BSON documents using the MongoDB Java driver implementation of BSON.

The code here was ported (copied, renamed, repackaged, modified) from the zjsonpatch project.

Description & Use-Cases

  • Java Library to find / apply JSON Patches according to RFC 6902.
  • JSON Patch defines a JSON document structure for representing changes to a JSON document.
  • It can be used to avoid sending a whole document when only a part has changed, thus reducing network bandwidth requirements if data (in json format) is required to send across multiple systems over network or in case of multi DC transfer.
  • This library compares two BsonValue inputs and produces a BsonArray of the changes.

Compatible with : Java 6 / 7 / 8


  • To find JsonPatch : Ω(N+M) ,N and M represents number of keys in first and second json respectively / O(summation of la*lb) where la , lb represents jsonArray of length la / lb of against same key in first and second json ,since LCS is used to find difference between 2 json arrays there of order of quadratic.
  • To Optimize Diffs ( compact move and remove into Move ) : Ω(D) / O(D*D) where D represents number of diffs obtained before compaction into Move operation.
  • To Apply Diff : O(D) where D represents number of diffs

How to use:

Current Version : 0.4.4

Add following to <dependencies/> section of your pom.xml -


API Usage

Obtaining Json Diff as patch

BsonArray patch = BsonDiff.asBson(BsonValue source, BsonValue target)

Computes and returns a JSON Patch (as a BsonArray) from source to target, Both source and target must be either valid BSON objects or arrays or values. Further, if resultant patch is applied to source, it will yield target.

The algorithm which computes this JsonPatch currently generates following operations as per rfc 6902 -

  • ADD
  • MOVE
  • COPY
  • TEST

Apply Json Patch

BsonValue target = BsonPatch.apply(BsonArray patch, BsonValue source);

Given a Patch, it apply it to source Bson and return a target Bson which can be ( Bson object or array or value ). This operation performed on a clone of source Bson ( thus, source Bson is untouched and can be used further).

To turn off MOVE & COPY Operations

EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone()
BsonArray patch = BsonDiff.asJson(BsonValue source, BsonValue target, flags)


First Json

{"a": 0,"b": [1,2]}

Second json ( the json to obtain )

 {"b": [1,2,0]}

Following patch will be returned:


here 'op' represents Operation, 'from' represent path from where value should be moved, 'path' represents where value should be moved.

Apply Json Patch In-Place

BsonPatch.applyInPlace(BsonArray patch, BsonValue source);

Given a patch, it will apply it to the source BSON mutating the instance, opposed to BsonPatch.apply which returns a new instance with the patch applied, leaving the source unchanged.


  1. 100+ selective hardcoded different input jsons , with their driver test classes present under /test directory.
  2. Apart from selective input, a deterministic random json generator is present under ( TestDataGenerator.java ), and its driver test class method is JsonDiffTest.testGeneratedJsonDiff().

*** Tests can only show presence of bugs and not their absence ***

Get Involved