I have two Json objects as below need to be compared. I am using Newtonsoft libraries for Json parsing.
string InstanceExpected = jsonExpected;
string InstanceAc
One option is to deserialize the json strings into C# objects and compare them.
This approach requires more work comparing to using JToken.DeepEquals
(as suggested by @JessedeWit), but has the advantage of giving better error messages if your tests fail (see screenshot below).
Your json string can be modelled into the following class:
public class Entity
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("objectId")]
public string ObjectId { get; set; }
}
In your test, deserialize the json strings into objects and compare them:
[TestFixture]
public class JsonTests
{
[Test]
public void JsonString_ShouldBeEqualAsExpected()
{
string jsonExpected = @"{ ""Name"": ""20181004164456"", ""objectId"": ""4ea9b00b-d601-44af-a990-3034af18fdb1%>"" }";
string jsonActual = @"{ ""Name"": ""AAAAAAAAAAAA"", ""objectId"": ""4ea9b00b-d601-44af-a990-3034af18fdb1%>"" }";
Entity expectedObject = JsonConvert.DeserializeObject(jsonExpected);
Entity actualObject = JsonConvert.DeserializeObject(jsonActual);
actualObject.Should().BeEquivalentTo(expectedObject);
}
}
PS: I used NUnit and FluentAssertions in my test method. Running the test: