-1

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 !!!

15
  • 2
    deserialize, compare, return the result Commented Nov 18, 2020 at 7:31
  • 1
    Note that there are no such things as a JSON objects. They are strings Commented Nov 18, 2020 at 7: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. Commented Nov 18, 2020 at 7:35
  • 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: 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. Commented Nov 18, 2020 at 8:19
  • 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. Commented Nov 18, 2020 at 8:58

1 Answer 1

0

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");
}
Sign up to request clarification or add additional context in comments.

4 Comments

If I were you, I would specify that you're using Newtonsoft.Json (newtonsoft.com/json), but otherwise great answer.
@Mafii, thanks for the input. I will remember this when I am posting new :)
@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, then I suggest using model and Jsonconvert.. I have updated the post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.