![]() ![]() Var jsonString = JsonSerializer. Private static JsonElement JsonElementFromObject(object obj) Private static JsonElement CreateDifferenceArray(JsonElement elem1, JsonElement elem2) Let's read the input JSON as JsonNode and compare: assertEquals(mapper.readTree(s1), mapper.readTree(s2)) It's important to know that two list elements are only compared as equal if they have the same values in the exact same order. Comparing JSON with JToken.DeepEquals Comparing JSON with JToken.DeepEquals This sample compares JToken instances using DeepEquals (JToken, JToken), comparing the token and all child tokens. Lenient mode As we know from the previous post that in Lenient mode extensibility will be allowed and no strict ordering will be checked. Return elem1.Equals(elem2) ? default : CreateDifferenceArray(elem1, elem2) Compare JSON Objects Using JSONassert Library Method assertEquals () JSONassert provides multiple static overloaded assertEquals () method. ![]() ![]() Return areArraysEqual ? default : CreateDifferenceArray(elem1, elem2) Var areArraysEqual = elem1.GetArrayLength() = elem2.GetArrayLength() įor (var i = 0 i < elem1.GetArrayLength() i++) Result.Add(prop.Name, CreateDifferenceArray(default, prop.Value)) Result.Add(prop.Name, CreateDifferenceArray(prop.Value, default)) įoreach (var prop in elem2.EnumerateObject().Where(prop => !elem1.TryGetProperty(prop.Name, out _))) If (diff.ValueKind != JsonValueKind.Undefined) Var diff = CompareJsonElements(prop.Value, value2) If (elem2.TryGetProperty(prop.Name, out var value2)) Return CreateDifferenceArray(elem1, elem2) įoreach (var prop in elem1.EnumerateObject()) Private static JsonElement CompareJsonElements(JsonElement elem1, JsonElement elem2) Using var doc2 = JsonDocument.Parse(json2) Using var doc1 = JsonDocument.Parse(json1) public static JsonElement CompareJsonObjects(string json1, string json2) The some method checks if any array elements pass a test (provided as a function). We will use similar strategy but recursively. If you use a more modern and need to recursively check and compare properties of complex objects, the code will be different. Similarly, the properties of the second object, which may not exist in the first object, must be handled similarly. Do the same if there is no corresponding property of the second object. The common strategy is to go through each property of the first JSON, find the corresponding property of the second JSON by its key, compare them and, if they are different, write them into the result. If (obj2.TryGetValue(property.Key, out var value2))ĭiff = new JArray(value1, value2) ĭiff = new JArray(value1, null) ĭiff = new JArray(null, property.Value) The answer will depend on which JSON library you want to use and whether you want to handle the nested properties of complex json objects, or whether it is sufficient to go through the list of properties of the main object.įor example, if you use the Newtonsoft.Json library and only compare first-level properties, this code would be enough: public static JObject CompareJsonObjects(string json1, string json2) ![]()
0 Comments
Leave a Reply. |