Philiprehberger.JsonMerge 0.3.0

dotnet add package Philiprehberger.JsonMerge --version 0.3.0
                    
NuGet\Install-Package Philiprehberger.JsonMerge -Version 0.3.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.JsonMerge" Version="0.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Philiprehberger.JsonMerge" Version="0.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Philiprehberger.JsonMerge" />
                    
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.JsonMerge --version 0.3.0
                    
#r "nuget: Philiprehberger.JsonMerge, 0.3.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.JsonMerge@0.3.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.JsonMerge&version=0.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Philiprehberger.JsonMerge&version=0.3.0
                    
Install as a Cake Tool

Philiprehberger.JsonMerge

CI NuGet Last updated

Deep merge JSON documents with configurable array strategies and null handling using System.Text.Json.

Installation

dotnet add package Philiprehberger.JsonMerge

Usage

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

var baseDoc = JsonNode.Parse("""{"name": "app", "db": {"host": "localhost", "port": 5432}}""");
var overrideDoc = JsonNode.Parse("""{"db": {"host": "production.db", "ssl": true}, "version": "2.0"}""");

var result = JsonMerge.Merge(baseDoc!, overrideDoc!);
// {"name": "app", "db": {"host": "production.db", "port": 5432, "ssl": true}, "version": "2.0"}

Custom Options

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

var options = new MergeOptions(ArrayStrategy.Concat, NullHandling.Remove);
var result = JsonMerge.Merge(baseDoc!, overrideDoc!, options);

Merge Multiple Documents

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

var result = JsonMerge.MergeAll(defaults, environment, local);

Array Element Matching by Key

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

var baseDoc = JsonNode.Parse("""{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}""");
var overrideDoc = JsonNode.Parse("""{"users": [{"id": 2, "name": "Robert"}]}""");

var options = new MergeOptions(ArrayMatchKey: "id");
var result = JsonMerge.Merge(baseDoc!, overrideDoc!, options);
// Users matched by "id": Bob becomes Robert, Alice unchanged

Merge Conflict Callback

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

var options = new MergeOptions(OnConflict: (path, left, right) =>
{
    // Custom resolution: keep the base value for "name" paths
    return path.EndsWith("name") ? left : right;
});
var result = JsonMerge.Merge(baseDoc!, overrideDoc!, options);

Path-Based Selective Merge

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

var options = new MergeOptions(PathFilter: new[] { "db.host", "version" });
var result = JsonMerge.Merge(baseDoc!, overrideDoc!, options);
// Only "db.host" and "version" are merged from override; other paths keep base values

RFC 7396 JSON Merge Patch

using System.Text.Json;
using Philiprehberger.JsonMerge;

var target = JsonDocument.Parse("""{"a": 1, "b": 2, "c": 3}""");
var patch = JsonDocument.Parse("""{"b": null, "c": 4, "d": 5}""");

var result = JsonMerge.MergePatch(target, patch);
// {"a": 1, "c": 4, "d": 5} — "b" removed by null, "c" updated, "d" added

API

JsonMerge

Method Description
Merge(JsonNode baseNode, JsonNode overrideNode) Deep merges two JSON documents. Override values win for scalars.
Merge(JsonNode baseNode, JsonNode overrideNode, MergeOptions options) Merges with custom options.
MergeAll(params JsonNode[] nodes) Merges multiple documents left to right.
MergePatch(JsonDocument target, JsonDocument patch) Applies an RFC 7396 JSON Merge Patch. Null values remove keys.

MergeOptions

Property Type Default Description
ArrayStrategy ArrayStrategy Replace How to handle array merges.
NullHandling NullHandling Keep How to handle null values.
ArrayMatchKey string? null Match array elements by this key for merging instead of positional.
OnConflict Func<string, JsonElement, JsonElement, JsonElement>? null Callback for custom conflict resolution with (path, base, override).
PathFilter IReadOnlyList<string>? null Only merge paths matching these patterns.

ArrayStrategy

Value Description
Replace Replace base array with override array.
Concat Concatenate both arrays.
Union Union of both arrays.

NullHandling

Value Description
Keep Keep null values in the result.
Remove Remove null values from the result.

Development

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

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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.3.0 132 4/1/2026
0.2.0 169 3/28/2026
0.1.2 111 3/25/2026
0.1.1 110 3/23/2026
0.1.0 108 3/23/2026