PQSoft.JsonComparer 1.0.0-beta.2

This is a prerelease version of PQSoft.JsonComparer.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package PQSoft.JsonComparer --version 1.0.0-beta.2
                    
NuGet\Install-Package PQSoft.JsonComparer -Version 1.0.0-beta.2
                    
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="PQSoft.JsonComparer" Version="1.0.0-beta.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PQSoft.JsonComparer" Version="1.0.0-beta.2" />
                    
Directory.Packages.props
<PackageReference Include="PQSoft.JsonComparer" />
                    
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 PQSoft.JsonComparer --version 1.0.0-beta.2
                    
#r "nuget: PQSoft.JsonComparer, 1.0.0-beta.2"
                    
#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 PQSoft.JsonComparer@1.0.0-beta.2
                    
#: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=PQSoft.JsonComparer&version=1.0.0-beta.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=PQSoft.JsonComparer&version=1.0.0-beta.2&prerelease
                    
Install as a Cake Tool

PQSoft.JsonComparer

A powerful library for comparing JSON documents with advanced features.

Features

  • Exact Match: Validates that two JSON documents are identical (except for tokens)
  • Subset Match: Verifies that all elements in the expected JSON exist within the actual JSON
  • Token Support: Extract values from actual JSON using tokens like [[TOKEN_NAME]]
  • Function Execution: Execute functions like {{GUID()}}, {{NOW()}}, and {{UTCNOW()}} during comparison
  • Variable Substitution: Substitute variables from provided context during preprocessing
  • Detailed Mismatch Reporting: Provides structured information on any differences found
  • Custom Function Registration: Extend functionality with custom functions

Installation

dotnet add package PQSoft.JsonComparer

Usage

Basic Exact Match

string expectedJson = """{ "id": "[[JOBID]]", "createdAt": "{{NOW()}}", "status": "complete" }""";
string actualJson = """{ "id": "12345", "createdAt": "2024-01-01T10:00:00.000+00:00", "status": "complete" }""";

bool isMatch = JsonComparer.ExactMatch(expectedJson, actualJson, out var extractedValues, out var mismatches);
// isMatch = true
// extractedValues["JOBID"] = "12345"

Subset Matching

string expectedSubset = """{ "status": "active", "type": "premium" }""";
string actualJson = """{ "id": 123, "name": "John", "status": "active", "type": "premium", "created": "2024-01-01" }""";

bool isSubset = JsonComparer.SubsetMatch(expectedSubset, actualJson, out var tokens, out var errors);
// isSubset = true - all expected fields exist in actual JSON

Token Extraction

string template = """{ "userId": "[[USER_ID]]", "orderId": "[[ORDER_ID]]", "total": "[[AMOUNT]]" }""";
string response = """{ "userId": "user_123", "orderId": "order_456", "total": "99.99" }""";

JsonComparer.ExactMatch(template, response, out var tokens, out var mismatches);
// tokens["USER_ID"] = "user_123"
// tokens["ORDER_ID"] = "order_456"
// tokens["AMOUNT"] = "99.99"

Function Execution

string template = """{
    "id": "{{GUID()}}",
    "timestamp": "{{UTCNOW()}}",
    "date": "{{NOW()}}"
}""";
string actual = """{
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "timestamp": "2024-01-01T10:00:00.000Z",
    "date": "2024-01-01T10:00:00.000+00:00"
}""";

bool matches = JsonComparer.ExactMatch(template, actual, out var values, out var errors);
// Functions are executed and compared against actual values

Variable Substitution

var variables = new Dictionary<string, object>
{
    ["BASE_URL"] = "https://api.example.com",
    ["API_VERSION"] = "v1",
    ["USER_ID"] = 12345
};

string expectedJson = """{
    "endpoint": "{{BASE_URL}}/{{API_VERSION}}/users/{{USER_ID}}",
    "userId": {{USER_ID}}
}""";

string actualJson = """{
    "endpoint": "https://api.example.com/v1/users/12345",
    "userId": 12345
}"""

// Variables are substituted before comparison
bool matches = JsonComparer.ExactMatch(expectedJson, actualJson, variables, out var tokens, out var mismatches);

Custom Functions

// Register custom function
JsonComparer.RegisterFunction("CUSTOM_DATE", () => DateTime.Now.ToString("yyyy-MM-dd"));

string template = """{ "processedDate": "{{CUSTOM_DATE()}}" }""";
string actual = """{ "processedDate": "2024-01-01" }""";

bool matches = JsonComparer.ExactMatch(template, actual, out var tokens, out var errors);

Error Handling

string expected = """{ "name": "John", "age": 30 }""";
string actual = """{ "name": "Jane", "age": 25 }""";

bool matches = JsonComparer.ExactMatch(expected, actual, out var tokens, out var mismatches);

if (!matches)
{
    foreach (var mismatch in mismatches)
    {
        Console.WriteLine($"Path: {mismatch.Path}");
        Console.WriteLine($"Expected: {mismatch.Expected}");
        Console.WriteLine($"Actual: {mismatch.Actual}");
    }
}
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 (2)

Showing the top 2 NuGet packages that depend on PQSoft.JsonComparer:

Package Downloads
PQSoft.JsonComparer.AwesomeAssertions

AwesomeAssertions extensions for JSON comparison that integrate with PQSoft.JsonComparer. Provides fluent assertion methods for JSON validation with token extraction and subset matching.

PQSoft.ReqNRoll

Reqnroll step definitions for API testing with PQSoft.HttpFile and PQSoft.JsonComparer

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0-beta.4 185 10/22/2025
1.2.0-beta.3 173 10/22/2025
1.2.0-beta.2 167 10/22/2025
1.2.0-beta.1 163 10/22/2025
1.1.0-beta.3 206 9/20/2025
1.1.0-beta.2 215 9/19/2025
1.1.0-beta.1 219 9/19/2025
1.0.0-beta.6 287 9/19/2025
1.0.0-beta.5 275 9/19/2025
1.0.0-beta.4 280 9/19/2025
1.0.0-beta.3 279 9/19/2025
1.0.0-beta.2 274 9/19/2025
1.0.0-beta.1 298 9/19/2025
1.0.0-alpha9 290 9/18/2025
1.0.0-alpha8 298 9/18/2025
1.0.0-alpha7 296 9/17/2025
1.0.0-alpha5 302 9/16/2025
1.0.0-alpha4 306 9/16/2025
1.0.0-alpha11 301 9/19/2025
1.0.0-alpha10 297 9/18/2025
Loading failed