How to Compare JSON Objects in C#

A complete guide to comparing JSON objects in C# with practical examples, Newtonsoft.Json, System.Text.Json usage, and performance considerations.

20 min read
Updated January 2024
Intermediate

This guide covers .NET 6+ with examples using Newtonsoft.Json and System.Text.Json libraries for robust JSON comparison.

Table of Contents

Understanding JSON in C#

In C#, JSON objects are typically represented using JObject/JToken (Newtonsoft.Json) or JsonElement/JsonNode (System.Text.Json). Each library provides its own approach to JSON parsing and comparison.

JSON Libraries in .NET

  • Newtonsoft.Json: JObject, JToken - Most popular and feature-rich
  • System.Text.Json: JsonElement, JsonNode - Built-in .NET library
  • System.Text.Json (Nodes): JsonObject, JsonArray - .NET 6+
  • DataContractJsonSerializer: .NET built-in serializer

Newtonsoft.Json Comparison

Newtonsoft.Json (Json.NET) is the most popular JSON library for .NET and provides robust comparison capabilities through JToken.DeepEquals() method.

using Newtonsoft.Json.Linq;
using System;

public class NewtonsoftJsonComparison
{
    public static void Main()
    {
        // Sample JSON strings
        string json1 = @"{""name"":""John"",""age"":30,""city"":""New York""}";
        string json2 = @"{""name"":""John"",""age"":30,""city"":""New York""}";
        string json3 = @"{""name"":""John"",""age"":31,""city"":""New York""}";

        // Parse JSON strings to JObjects
        JObject obj1 = JObject.Parse(json1);
        JObject obj2 = JObject.Parse(json2);
        JObject obj3 = JObject.Parse(json3);

        // Compare JObjects
        Console.WriteLine($"obj1 equals obj2: {JToken.DeepEquals(obj1, obj2)}"); // True
        Console.WriteLine($"obj1 equals obj3: {JToken.DeepEquals(obj1, obj3)}"); // False

        // Deep comparison with nested objects
        string nestedJson1 = @"{""user"":{""name"":""Alice"",""age"":25},""settings"":{""theme"":""dark""}}";
        string nestedJson2 = @"{""user"":{""name"":""Alice"",""age"":25},""settings"":{""theme"":""dark""}}";

        JObject nested1 = JObject.Parse(nestedJson1);
        JObject nested2 = JObject.Parse(nestedJson2);

        Console.WriteLine($"Nested objects equal: {JToken.DeepEquals(nested1, nested2)}"); // True

        // Array comparison
        string arrayJson1 = @"{""items"":[1,2,3]}";
        string arrayJson2 = @"{""items"":[1,2,3]}";
        string arrayJson3 = @"{""items"":[1,3,2]}";

        JObject array1 = JObject.Parse(arrayJson1);
        JObject array2 = JObject.Parse(arrayJson2);
        JObject array3 = JObject.Parse(arrayJson3);

        Console.WriteLine($"Arrays equal (same order): {JToken.DeepEquals(array1, array2)}"); // True
        Console.WriteLine($"Arrays equal (different order): {JToken.DeepEquals(array1, array3)}"); // False
    }
}

Newtonsoft.Json Advantage

JToken.DeepEquals() performs deep comparison automatically and handles all JSON types correctly, including arrays, objects, and primitive values with proper type handling.

Summary

Comparing JSON objects in C# requires choosing the right library for your needs. Newtonsoft.Json provides the most comprehensive solution, while System.Text.Json offers built-in performance and modern features.

Key Takeaways

  • Use JToken.DeepEquals() for Newtonsoft.Json comparison
  • System.Text.Json requires custom comparison logic
  • Handle data type conversions carefully
  • Consider performance with large JSON structures
  • Use proper error handling for JSON parsing
  • Test edge cases like null values and empty objects

Recommended Tools

  • Newtonsoft.Json: Most comprehensive and flexible
  • System.Text.Json: Built-in and performant
  • Json.NET Compare: Advanced comparison features
  • FluentAssertions: For testing JSON equality
  • JsonDiffPatch: For diff and patch operations