NOTE the following libraries:
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
I'm not entirely sure I understand your question correctly.
I am assuming you are trying to identify what keys are missing from the actual JSON.
If you are just interested in the missing KEYS the below code will help you, if not, please provide an example of the types of differences you are trying to identify.
public IEnumerable<JProperty> DoCompare(string expectedJSON, string actualJSON)
{
// convert JSON to object
JObject xptJson = JObject.Parse(expectedJSON);
JObject actualJson = JObject.Parse(actualJSON);
// read properties
var xptProps = xptJson.Properties().ToList();
var actProps = actualJson.Properties().ToList();
// find missing properties
var missingProps = xptProps.Where(expected => actProps.Where(actual => actual.Name == expected.Name).Count() == 0);
return missingProps;
}
NOTE that if the this method returns an empty IEnumerable then the ACTUAL JSON has all the keys required according to the structure of the expected JSON.
NOTE: the actual JSON could still have more keys that the expected JSON does not required.
to explain my notes further...
assume your expected JSON is:
{ Id: 1, Name: "Item One", Value: "Sample" }
and that your ACTUAL JSON is :
{ Id: 1, Name: "Item One", SomeProp: "x" }
the above function will tell you that the Value key is missing, but will not mention anything about the SomeProp key...unless your swap the input parameters around.