Philiprehberger.JsonPatch 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Philiprehberger.JsonPatch --version 0.1.0
                    
NuGet\Install-Package Philiprehberger.JsonPatch -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Philiprehberger.JsonPatch" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.JsonPatch" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.JsonPatch" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Philiprehberger.JsonPatch --version 0.1.0
                    
#r "nuget: Philiprehberger.JsonPatch, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Philiprehberger.JsonPatch@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Philiprehberger.JsonPatch&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.JsonPatch&version=0.1.0
                    
Install as a Cake Tool

Philiprehberger.JsonPatch

CI NuGet License

RFC 6902 JSON Patch operations for System.Text.Json — apply, generate, and parse add/remove/replace/move/copy/test patches.

Installation

dotnet add package Philiprehberger.JsonPatch

Usage

Apply a Patch

using System.Text.Json.Nodes;
using Philiprehberger.JsonPatch;

var document = JsonNode.Parse("""{"name": "Alice", "age": 30}""");

var operations = new[]
{
    PatchOperation.Replace("/name", JsonValue.Create("Bob")),
    PatchOperation.Add("/email", JsonValue.Create("bob@example.com")),
    PatchOperation.Remove("/age")
};

var result = JsonPatch.Apply(document, operations);
// {"name":"Bob","email":"bob@example.com"}

Generate a Diff

using System.Text.Json.Nodes;
using Philiprehberger.JsonPatch;

var before = JsonNode.Parse("""{"name": "Alice", "age": 30}""");
var after = JsonNode.Parse("""{"name": "Alice", "age": 31, "email": "alice@example.com"}""");

var operations = JsonPatch.Diff(before, after);

foreach (var op in operations)
    Console.WriteLine($"{op.Op} {op.Path}");
// replace /age
// add /email

Parse from JSON

using Philiprehberger.JsonPatch;

var json = """
[
  { "op": "replace", "path": "/name", "value": "Bob" },
  { "op": "remove", "path": "/age" }
]
""";

var patchDoc = JsonPatch.Parse(json);
var result = patchDoc.Apply(JsonNode.Parse("""{"name": "Alice", "age": 30}"""));
// {"name":"Bob"}

Test Operations

using System.Text.Json.Nodes;
using Philiprehberger.JsonPatch;

var document = JsonNode.Parse("""{"name": "Alice"}""");

// Test passes — value matches
var ops = new[] { PatchOperation.Test("/name", JsonValue.Create("Alice")) };
var result = JsonPatch.Apply(document, ops);

// Test fails — throws InvalidOperationException
var failing = new[] { PatchOperation.Test("/name", JsonValue.Create("Bob")) };
JsonPatch.Apply(document, failing); // throws

API

JsonPatch

Method Description
Apply(JsonNode?, IEnumerable<PatchOperation>) Apply patch operations to a document, returning a modified copy
Diff(JsonNode?, JsonNode?) Generate patch operations that transform one document into another
Parse(string) Parse a JSON array of RFC 6902 operations into a JsonPatchDocument

JsonPatchDocument

Member Description
JsonPatchDocument(IEnumerable<PatchOperation>) Create a patch document from a sequence of operations
Operations Read-only list of patch operations
Apply(JsonNode?) Apply all operations to a document copy

PatchOperation

Member Description
Op Operation type (add, remove, replace, move, copy, test)
Path Target JSON Pointer path
From Source path for move/copy operations
Value Value for add/replace/test operations
Add(string, JsonNode?) Create an add operation
Remove(string) Create a remove operation
Replace(string, JsonNode?) Create a replace operation
Move(string, string) Create a move operation
Copy(string, string) Create a copy operation
Test(string, JsonNode?) Create a test operation

Development

dotnet build src/Philiprehberger.JsonPatch.csproj --configuration Release

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.1 137 4/1/2026
0.2.0 172 3/28/2026
0.1.1 113 3/23/2026
0.1.0 109 3/21/2026