0

I have a two json objects like this below,

Json 1:

{
"data": {
  "firstName": "xxx",
  "lastName": "yyy",
  "age": "29"
}
}

Json 2:

{
 "data": {
    "firstName": "aaa",
    "lastName": "yyy",
    "age": "30",
    "location": "USA"
},
"meta": {
  "browser": "chrome",
  "ip": "999.999.999"
    }
}

How can i compare properties of json 1 with json 2 and return the bool value if values are equal ?

From the above example the firstName value is different in both json objects so the result will return bool value as false , otherwise it will return true.

Please help, thanks in advance !!!

DiplomacyNotWar
  • 31,605
  • 6
  • 57
  • 75
Vicky
  • 27
  • 9
  • 1
    deserialize, compare, return the result – Cid Nov 18 '20 at 07:31
  • 1
    Note that there are no such things as a JSON **objects**. They are strings – Cid Nov 18 '20 at 07:32
  • Please note that the `[asp.net]` tag is for ASP.NET (Framework) and `[asp.net-core]` is for ASP.NET (Core). I would expect them to be mutually exclusive, so seeing both is a little strange and can make questions unclear. They aren't relevant to this question anyway, so I've removed them. – DiplomacyNotWar Nov 18 '20 at 07:35
  • Json 1 will change randomly for the next time , How can i deserialize it to c# object ? – Vicky Nov 18 '20 at 07:36
  • What changes are you trying to detect here? All changes? What is the goal once you've detected them? – DiplomacyNotWar Nov 18 '20 at 07:37
  • *"How can i deserialize it to c# object ?"* @Vicky since you're using json.net, check [this](https://www.newtonsoft.com/json/help/html/DeserializeObject.htm) – Cid Nov 18 '20 at 07:40
  • Json 1 will comes from validation form which is randomly change , i have to compare that json 1 with Json 2 which is from database so if the property values are same then it should return true and allows user to access the application – Vicky Nov 18 '20 at 07:40
  • Json 1 will change dynamically on every request, so i cannot create a c# class to deserialize it @Cid – Vicky Nov 18 '20 at 07:42
  • @Vicky then, check [this](https://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm) – Cid Nov 18 '20 at 07:43
  • Possible dupe : [Deserialize json object into dynamic object using Json.net](https://stackoverflow.com/questions/4535840/deserialize-json-object-into-dynamic-object-using-json-net) – Cid Nov 18 '20 at 07:44
  • On second request, Json 1 property will change like maybe it will not contain firstName property and only contains lastName and age , so i think deserialize and comparing is difficult – Vicky Nov 18 '20 at 07:47
  • you can always [check if the property exists in a dynamic object](https://stackoverflow.com/questions/9956648/how-do-i-check-if-a-property-exists-on-a-dynamic-anonymous-type-in-c) – Cid Nov 18 '20 at 07:52
  • 1
    @Cid: I think it's *entirely* reasonable to refer to JSON objects. Would you object to someone using the term "XML element"? A "JSON object" is a parallel concept to that - and "object" is the term used in the JSON RFC: https://tools.ietf.org/html/rfc8259#section-4. A JSON object could be represented in code without deserializing to a schema-specific .NET class using `JObject` in Json.NET, for example. – Jon Skeet Nov 18 '20 at 08:19
  • @JonSkeet I tend to disagree, since they are objects representations, not object themselves (although strings are objects), but this is just semantic about the Jon Skeet Object Notation :) – Cid Nov 18 '20 at 08:55
  • 1
    @Cid: They're entities referred to in the JSON RFC as "objects". So while referring to them *in the context of JSON* (rather than in the context of a programming language representation) I think it's fine to refer to them as objects. They *are* objects in that context. Again, do you try to "correct" anyone referring to XML elements? It's exactly the same, except that "object" is overloaded in this case where "element" isn't. – Jon Skeet Nov 18 '20 at 08:58

1 Answers1

1

Approach #1 - Using JObject from Newtonsoft.Json Library

var json1 = File.ReadAllText("json1.json");
var json2 = File.ReadAllText("json2.json");

var jObj1 = JObject.Parse(json1);
var jObj2 = JObject.Parse(json2);

if (jObj1["data"]["firstName"] != null && jObj2["data"]["firstName"] != null && jObj1["data"]["firstName"].ToString() == jObj2["data"]["firstName"].ToString())
{
    //condition is true
    Console.WriteLine("true");
}
else
{
    //condition is false
    Console.WriteLine("false");
}

Approach #2 - Using Modal & JsonConvert from Newtonsoft.Json Library. I have used json2csharp to convert the json to c# class

public class Data
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string age { get; set; }
    public string location { get; set; }
}

public class Meta
{
    public string browser { get; set; }
    public string ip { get; set; }
}

public class Root
{
    public Data data { get; set; }
    public Meta meta { get; set; }
}

var jObj1 = JsonConvert.DeserializeObject<Root>(json1);
var jObj2 = JsonConvert.DeserializeObject<Root>(json2);

if (jObj1.data.firstName.Equals(jObj2.data.firstName))
{
    Console.WriteLine("true");
}
else
{
    Console.WriteLine("false");
}
Krishna Varma
  • 3,339
  • 2
  • 6
  • 23
  • If I were you, I would specify that you're using Newtonsoft.Json (https://www.newtonsoft.com/json), but otherwise great answer. – Mafii Nov 18 '20 at 08:06
  • @Mafii, thanks for the input. I will remember this when I am posting new :) – Krishna Varma Nov 18 '20 at 08:10
  • @KrishnaMuppalla in my scenario json 1 properties will randomly change like on second request it may (or) may not contain firstName property . At that time i will get error while executing if condition . – Vicky Nov 18 '20 at 08:36
  • @Vicky, then I suggest using model and Jsonconvert.. I have updated the post – Krishna Varma Nov 18 '20 at 08:46