This project provides a simple way to validate JSON objects in dotnet. The main use for the tool is in integration tests.
$ dotnet add package JsonValidator.ShouldlyJsonValidator.Shouldly package depends on and uses both Shouldly and the basic JsonValidator package.Should...() pattern.System.Text.Json.JsonDocument: ShouldMatch(...) checks whether the JSON document matches the input object in both structure and values.System.Net.Http.HttpResponseMessage: ShouldHaveJsonBody(...) checks whether the HTTP response's body as a JSON string matches the input object in both structure and values.For JsonDocument:
var json = JsonDocument.Parse(myJsonString);
json.ShouldMatch(
new
{
name = "Anna Smith",
level = 2,
});For HttpResponseMessage:
HttpResponseMessage response = await _client.SendAsync(request);
response.ShouldHaveJsonBody(
new
{
name = "Anna Smith",
level = 2,
});