MCPcopy Index your code
hub / github.com/flipkart-incubator/zjsonpatch

github.com/flipkart-incubator/zjsonpatch @0.6.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.6.2 ↗ · + Follow
538 symbols 2,063 edges 84 files 84 documented · 16% 3 cross-repo links updated 3mo ago0.6.2 · 2025-12-15★ 57932 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

CircleCI Join the chat at https://gitter.im/zjsonpatch/community

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

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.
  • Extended JSON pointer functionality (i.e. reference array elements via a key): /array/id=123/data
  • The user has to ensure that a unique field is used as a reference key. Should there be more than one array element matching the given key-value pair, the first element will be selected.
  • Key based referencing may be slow for large arrays. Hence, standard index based array pointers should be used for large arrays.

Compatible with : Java 17+ versions

Jackson Version Support

  • Jackson 2.x: JSON processing using com.fasterxml.jackson.* APIs
  • Jackson 3.x: Full compatibility using tools.jackson.* APIs
  • Both Jackson versions can coexist as optional dependencies

⚠️ Breaking Changes in 0.6.0

Jackson Dependencies are now Optional

Starting with version 0.6.0, all Jackson dependencies are marked as <optional>true</optional> to allow consumers to choose between Jackson 2.x and Jackson 3.x versions.

Migration Required for Existing Users:

If you were relying on zjsonpatch to transitively provide Jackson dependencies, you must now explicitly add Jackson to your project:


<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.18.2</version>
</dependency>

Why this change? - Prevents version conflicts when projects use different Jackson versions - Allows explicit choice between Jackson 2.x and Jackson 3.x - Provides cleaner dependency management for consumers - Enables Jackson 3.x support without forcing version upgrades

Impact: Projects that don't explicitly declare Jackson dependencies will get compilation errors until Jackson is added to their dependencies.

Code Coverage

Package Class, % Method, % Line, %
all classes 100% (6/ 6) 93.6% (44/ 47) 96.2% (332/ 345)

Complexity

  • 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.6.0

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

For Jackson 2.x support only

<dependency>
    <groupId>io.github.vishwakarma</groupId>
    <artifactId>zjsonpatch</artifactId>
    <version>{version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.18.2</version>
</dependency>

For Jackson 3.x support only

<dependency>
    <groupId>io.github.vishwakarma</groupId>
    <artifactId>zjsonpatch</artifactId>
    <version>{version}</version>
</dependency>
<dependency>
    <groupId>tools.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>3.0.0</version>
</dependency>

For both Jackson 2.x and 3.x support

<dependency>
    <groupId>io.github.vishwakarma</groupId>
    <artifactId>zjsonpatch</artifactId>
    <version>{version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.18.2</version>
</dependency>
<dependency>
    <groupId>tools.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>3.0.0</version>
</dependency>

API Usage

Jackson Version Selection

You must choose the correct public API classes based on your Jackson version:

  • Use JsonDiff/JsonPatch for Jackson 2.x
  • Use Jackson3JsonDiff/Jackson3JsonPatch for Jackson 3.x

Why separate APIs? The original public API is based on Jackson 2.x types and cannot be changed without breaking compatibility. A unified API containing both versions is not possible since only one Jackson version may be available in the classpath, and the versions use incompatible package structures.

Each API automatically uses its corresponding Jackson version internally. The library detects the actual Jackson version of nodes by checking if they are instances of specific JsonNode types, properly handling inheritance and ensuring correct operation even in mixed-version scenarios.

Obtaining JSON Diff as patch

Jackson 2.x API

JsonNode patch = JsonDiff.asJson(JsonNode source, JsonNode target)

Jackson 3.x API

JsonNode patch = Jackson3JsonDiff.asJson(JsonNode source, JsonNode target)

Computes and returns a JSON patch from source to target, Both source and target must be either valid JSON 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

Jackson 2.x API

JsonNode target = JsonPatch.apply(JsonNode patch, JsonNode source);

Jackson 3.x API

JsonNode target = Jackson3JsonPatch.apply(JsonNode patch, JsonNode source);

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

To turn off MOVE & COPY Operations

Jackson 2.x API

EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone();
JsonNode patch = JsonDiff.asJson(JsonNode source, JsonNode target, flags);

Jackson 3.x API

EnumSet<DiffFlags> flags = DiffFlags.dontNormalizeOpIntoMoveAndCopy().clone();
JsonNode patch = Jackson3JsonDiff.asJson(JsonNode source, JsonNode target, flags);

Example

First Json

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

Second json ( the json to obtain )

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

Following patch will be returned:

[{"op":"move","from":"/a","path":"/b/2"}]

here "op" specifies the operation ("move"), "from" specifies the path from where the value should be moved, and "path" specifies where value should be moved. The value that is moved is taken as the content at the "from" path.

Extended JSON Pointer Example

JSON

{
  "a": [
    {
      "id": 1,
      "data": "abc"
    },
    {
      "id": 2,
      "data": "def"
    }
  ]
}

JSON path

/a/id=2/data

Following JSON would be returned

"def"

Apply Json Patch In-Place

Jackson 2.x API

JsonPatch.applyInPlace(JsonNode patch, JsonNode source);

Jackson 3.x API

Jackson3JsonPatch.applyInPlace(JsonNode patch, JsonNode source);

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

This is an extension to the RFC, and has some additional limitations. Specifically, the source document cannot be fully change in place (the Jackson APIs do not support that level of mutability). This means the following operations are not supported: * remove with an empty or root path; * replace with an empty or root path; * move, add or copy targeting an empty or root path.

Tests:

  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().
  3. Full Jackson 3.x compatibility test suite with Jackson3* test classes that reflect all Jackson 2.x functionality.

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

Extension points exported contracts — how you extend this code

ObjectNodeWrapper (Interface)
@author Mariusz Sondecki @since 0.6.0 [4 implementers]
src/main/java/com/flipkart/zjsonpatch/mapping/ObjectNodeWrapper.java
ArrayNodeWrapper (Interface)
Wrapper interface for array node operations. @author Mariusz Sondecki @since 0.6.0 [4 implementers]
src/main/java/com/flipkart/zjsonpatch/mapping/ArrayNodeWrapper.java
JsonNodeWrapper (Interface)
@author Mariusz Sondecki @since 0.6.0 [4 implementers]
src/main/java/com/flipkart/zjsonpatch/mapping/JsonNodeWrapper.java
JsonNodeFactoryWrapper (Interface)
(no doc) [4 implementers]
src/main/java/com/flipkart/zjsonpatch/mapping/JsonNodeFactoryWrapper.java

Core symbols most depended-on inside this repo

get
called by 285
src/main/java/com/flipkart/zjsonpatch/mapping/JsonNodeWrapper.java
size
called by 94
src/main/java/com/flipkart/zjsonpatch/mapping/JsonNodeWrapper.java
toString
called by 62
src/main/java/com/flipkart/zjsonpatch/mapping/JsonNodeWrapper.java
parse
called by 56
src/main/java/com/flipkart/zjsonpatch/JsonPointer.java
parse
called by 53
src/main/java/com/flipkart/zjsonpatch/Jackson3JsonPointer.java
evaluate
called by 52
src/main/java/com/flipkart/zjsonpatch/JsonPointer.java
textValue
called by 42
src/main/java/com/flipkart/zjsonpatch/mapping/JsonNodeWrapper.java
isArrayIndex
called by 41
src/main/java/com/flipkart/zjsonpatch/RefToken.java

Shape

Method 454
Class 72
Enum 7
Interface 5

Languages

Java100%

Modules by API surface

src/main/java/com/flipkart/zjsonpatch/AbstractJsonDiff.java26 symbols
src/main/java/com/flipkart/zjsonpatch/mapping/jackson3/Jackson3NodeWrapper.java25 symbols
src/main/java/com/flipkart/zjsonpatch/mapping/jackson2/Jackson2NodeWrapper.java25 symbols
src/test/java/com/flipkart/zjsonpatch/JsonPointerTest.java22 symbols
src/test/java/com/flipkart/zjsonpatch/Jackson3JsonPointerTest.java22 symbols
src/main/java/com/flipkart/zjsonpatch/mapping/JsonNodeWrapper.java21 symbols
src/main/java/com/flipkart/zjsonpatch/AbstractJsonPointer.java17 symbols
src/main/java/com/flipkart/zjsonpatch/mapping/JacksonVersionBridge.java16 symbols
src/main/java/com/flipkart/zjsonpatch/InPlaceApplyProcessor.java14 symbols
src/test/java/com/flipkart/zjsonpatch/ApiTest.java13 symbols
src/main/java/com/flipkart/zjsonpatch/RefToken.java13 symbols
src/test/java/com/flipkart/zjsonpatch/Jackson3JsonDiffTest.java12 symbols

For agents

$ claude mcp add zjsonpatch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page