Skip to content


Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

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.
  • When used in combination with the HTTP PATCH method as per RFC 5789 HTTP PATCH, it will do partial updates for HTTP APIs in a standard way.
  • This library compares two BsonValue inputs and produces a BsonArray of the changes.

Compatible with : Java 8 and above all versions


  • 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 JSON Array 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.12

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
  • remove
  • replace
  • move
  • copy

Apply Json Patch

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

Given a patch, 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 the operation ("move"), "from" represent path from where value should be moved, "path" represents where value should be moved. The value that is moved is taken as the content at the "from" path.

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 ( ), and its driver test class method is JsonDiffTest.testGeneratedJsonDiff().

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

Get Involved