JsonScrutinize 2.0.1

dotnet add package JsonScrutinize --version 2.0.1                
NuGet\Install-Package JsonScrutinize -Version 2.0.1                
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="JsonScrutinize" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonScrutinize --version 2.0.1                
#r "nuget: JsonScrutinize, 2.0.1"                
#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.
// Install JsonScrutinize as a Cake Addin
#addin nuget:?package=JsonScrutinize&version=2.0.1

// Install JsonScrutinize as a Cake Tool
#tool nuget:?package=JsonScrutinize&version=2.0.1                

JSONScrutinize

JSONScrutinize is a powerful .NET library designed to validate and scrutinize JSON strings against predefined C# class definitions. It provides deep validation of JSON structures, detecting mismatches in data types, missing or null keys, and enforcing validation rules using attributes like [Required] and [RegularExpression].

Features

  • Type Validation: Compares JSON property types with their corresponding C# class property types and flags any mismatches.
  • Missing Keys Detection: Identifies missing properties based on [Required] attributes.
  • Null Key Detection: Flags properties with null values unless explicitly ignored.
  • Regex Validation: Ensures properties adhere to specified formats using [RegularExpression] attributes.
  • Nested Objects & Arrays: Recursively validates nested structures, ensuring type integrity at all levels.

Installation

To install JSONScrutinize, add the required package via NuGet:

Install-Package Newtonsoft.Json

Usage

Standard Class Definition

Define a C# class with validation attributes:

using System.ComponentModel.DataAnnotations;

public class StandardClass
{
    [Required]
    public string key1 { get; set; }
    
    [RegularExpression(@"^[0-9]{3}$")]
    public string key2 { get; set; }
    
    public string key3 { get; set; }
}

JSON Input Example

{
    "key1": "value1",
    "key2": "123",
    "key3": 123
}

In this case, key3 should be a string, but an integer is provided, which will be flagged as an error.

Running Validation

using JsonScrutinize;

StandardClass standard = new StandardClass();
string json = "{\"key1\": \"value1\", \"key2\": \"123\", \"key3\": 123}";
Type typeToCheck = standard.GetType();

JsonScrutinizer jsonScrutinize = new JsonScrutinize();
var result = await jsonScrutinize.KeyTypeScrutinize(json, typeToCheck);

Expected Output

{
    "StatusCode": "S201",
    "Description": "Found 1 mismatched keys in the provided JSON",
    "MismatchedKeys": [
        "key3"
    ]
}

This indicates that key3 does not match the expected type.

Nested Objects & Arrays

Standard Class for Nested Objects

public class ParentClass
{
    [Required]
    public string parentKey { get; set; }
    
    public ChildClass child { get; set; }
}

public class ChildClass
{
    [RegularExpression(@"^[A-Za-z]{3}$")]
    public string childKey { get; set; }
}

JSON Input for Nested Objects

{
    "parentKey": "ParentValue",
    "child": {
        "childKey": "AB12"
    }
}

Expected Output

{
    "StatusCode": "S201",
    "Description": "Found 1 mismatched keys in the provided JSON",
    "MismatchedKeys": [
        "child.childKey"
    ]
}

Status Codes & Their Meanings

Status Code Meaning
S200 Success - No issues found
S201 Type mismatch found
S202 Null keys detected
S203 Missing keys detected
E6001 JSON input is null or empty
E6002 Type to compare is null
E6005 JSON parsing error or unexpected failure

Additional Validation Functions

Missing Key Check

var missingKeysResult = await jsonScrutinize.KeyMissingScrutinize(json, typeToCheck);

Null Key Check (Ignoring Certain Keys)

var nullKeysResult = await jsonScrutinize.KeyNullScrutinize(json, typeToCheck, "key3");

Version Information

Release Date Version Changes Done Submitted By
2025-02-04 v2.0.1 Changed JsonScrutinize namespace to JsonScrutinizer for better readability Core Team
2025-02-04 v2.0.0 New release with type validation, null and missing key detection, and regex validation support Core Team
2025-02-03 v1.0.0 Initial release with type validation only Core Team

Conclusion

JSONScrutinize offers a comprehensive way to validate JSON structures against standard class definitions in .NET applications. Whether enforcing data types, required fields, or specific formats, this library ensures data integrity effectively.

For contributions, issues, or improvements, feel free to submit to the GitHub repository.

Happy Coding!

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.1 83 2/4/2025
2.0.0 65 2/4/2025
1.0.4 78 2/3/2025
1.0.3 78 2/3/2025
1.0.2 62 2/3/2025
1.0.1 82 2/3/2025
1.0.0 79 2/3/2025