JsonMigrations 3.0.0

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

JsonMigrations

Latest Release Latest Pre-Release

A .NET library for incrementally migrating JSON files (such as appsettings.json) without needing to overwrite them entirely. This is useful when JSON files contain installation-specific values that cannot be known in advance.

Migrations are plain C# classes that run in a defined order. After each run the library records which migration was last applied, so only newer migrations execute on subsequent runs.

Getting Started

Installation

Install via NuGet:

dotnet add package JsonMigrations

Requirements: .NET 6.0 or later

Quick Example

using System.Text.Encodings.Web;
using System.Text.Json;
using JsonMigrations;
using JsonMigrations.Enums;

// 1. Configure options (optional — defaults shown below)
var options = new JsonMigratorOptions()
{
    InvalidJsonAction = InvalidJsonAction.Throw,
    FileNotExistAction = FileAction.Throw,
    MigrationFaultAction = MigrationAction.Throw,
    JsonSerializerOptions = new JsonSerializerOptions()
    {
        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
        WriteIndented = true,
    }
};
JsonMigrator.SetOptions(options);

// 2. Register migrations by assembly or namespace
JsonMigrator.AddJsonMigrations(typeof(MyFirstMigration).Assembly);
// or: JsonMigrator.AddJsonMigrations(typeof(MyFirstMigration).Namespace!);

// 3. Run migrations on one or more files
JsonMigrator.Migrate("AppsettingsProgram", "appsettings.json", "appsettings.development.json");

A complete working example is available in the Example project.

Writing Migrations

Create a class that implements IJsonMigration:

using System.Text.Json;
using System.Text.Json.Nodes;
using JsonMigrations.Interfaces;

public class MyFirstMigration : IJsonMigration
{
    public int Order => 0;
    public string MigrationKey => "AppsettingsProgram";

    public void Up(JsonObject jsonObject, JsonSerializerOptions options)
    {
        jsonObject.TryAdd("ConnectionString", "Server=localhost;Database=mydb");
    }
}
Property Description
Order Determines execution sequence. Lowest runs first. Each new migration must have a higher order than previous ones.
MigrationKey Groups migrations together. Only migrations matching the key passed to Migrate() will run.
Up The migration logic. Modify the JsonObject to add, update, or remove properties. The JsonSerializerOptions from configuration are passed through.

After running, a "Migration" property is written to each JSON file with the class name of the last applied migration. On subsequent runs, only migrations with a higher Order are executed.

Configuration

JsonMigratorOptions

Option Type Default Description
MigrationFaultAction MigrationAction Throw What happens when a migration throws an exception.
InvalidJsonAction InvalidJsonAction Throw What happens when a file contains invalid JSON.
FileNotExistAction FileAction Throw What happens when a target file does not exist.
JsonSerializerOptions JsonSerializerOptions Indented, relaxed encoding Serializer options used for reading/writing JSON and available inside migrations.

Enum Values

MigrationAction — behaviour on migration failure: | Value | Description | |---|---| | Ignore | Skips the failed migration and continues with the next one. | | Throw | Throws the exception. | | Restore | Restores the JSON file to its state before the migration run started. |

InvalidJsonAction — behaviour on invalid JSON: | Value | Description | |---|---| | None | Skips the file. | | Throw | Throws an InvalidJsonException. |

FileAction — behaviour when a file does not exist: | Value | Description | |---|---| | None | Skips the file. | | Create | Creates the file with an empty JSON object ({}). | | Throw | Throws an exception. |

API Reference

// Configure options (call before Migrate)
JsonMigrator.SetOptions(JsonMigratorOptions options);

// Register migrations from one or more assemblies
JsonMigrator.AddJsonMigrations(params Assembly[] assemblies);

// Register migrations from one or more namespaces
JsonMigrator.AddJsonMigrations(params string[] namespaces);

// Run pending migrations for a key against one or more files
JsonMigrator.Migrate(string migrationKey, params string[] filePaths);

Contributing

See CONTRIBUTING.md for guidelines on how to contribute, including commit conventions and build instructions.

License

See LICENSE.txt for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.
  • net6.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
3.0.0 90 3/21/2026
2.0.0 338 5/25/2023
1.0.2 382 2/26/2023
1.0.1 376 2/26/2023
1.0.0 407 2/25/2023
0.1.4 90 3/21/2026
0.1.3-pre 86 3/21/2026