Search code examples
c#json.netfluent-assertions

How to exclude a JSON property from comparison


How to exclude a JSON property from comparison in FluentAssertions.Json?

JToken token, expectedJson;
token.Should().BeEquivalentTo(expectedJson);
{
  "property1":"value1",
  "property2":"value2",
  "property3":"value3"
}

I want to exclude property with name "property2" from the comparison. How is it possible?


Solution

  • I resolved it by preprocessing of token and expectedJson.

    JToken token, expectedJson;
    ((JValue)expectedJson.SelectToken("property2")).Value = null;
    ((JValue)token.SelectToken("property2")).Value = null;
    token.Should().BeEquivalentTo(expectedJson);