How to get formatted JSON in .NET using C#?

JSON formatting in C# allows you to control how JSON data is presented when serialized. The Newtonsoft.Json library provides formatting options through the Formatting enumeration to make JSON output more readable or compact as needed.

Syntax

Following is the syntax for serializing objects with different formatting options −

string json = JsonConvert.SerializeObject(object, Formatting.Indented);
string json = JsonConvert.SerializeObject(object, Formatting.None);

Formatting Options

Option Description Use Case
Formatting.None No special formatting is applied. This is the default. Compact JSON for APIs and storage
Formatting.Indented Child objects are indented for better readability Human-readable JSON for debugging

Using Formatting.Indented

The Formatting.Indented option creates well-formatted, readable JSON with proper indentation −

using System;
using Newtonsoft.Json;

class Product {
    public string[] Sizes { get; set; }
    public decimal Price { get; set; }
    public DateTime Expiry { get; set; }
    public string Name { get; set; }
}

class Program {
    static void Main(string[] args) {
        Product product = new Product {
            Name = "Apple",
            Expiry = new DateTime(2008, 12, 28),
            Price = 3.9900M,
            Sizes = new[] { "Small", "Medium", "Large" }
        };
        
        string json = JsonConvert.SerializeObject(product, Formatting.Indented);
        Console.WriteLine("Formatted JSON:");
        Console.WriteLine(json);
        
        Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
        Console.WriteLine("\nDeserialized Name: " + deserializedProduct.Name);
    }
}

The output of the above code is −

Formatted JSON:
{
  "Sizes": [
    "Small",
    "Medium",
    "Large"
  ],
  "Price": 3.9900,
  "Expiry": "2008-12-28T00:00:00",
  "Name": "Apple"
}

Deserialized Name: Apple

Using Formatting.None

The Formatting.None option creates compact JSON without any whitespace or indentation −

using System;
using Newtonsoft.Json;

class Product {
    public string[] Sizes { get; set; }
    public decimal Price { get; set; }
    public DateTime Expiry { get; set; }
    public string Name { get; set; }
}

class Program {
    static void Main(string[] args) {
        Product product = new Product {
            Name = "Apple",
            Expiry = new DateTime(2008, 12, 28),
            Price = 3.9900M,
            Sizes = new[] { "Small", "Medium", "Large" }
        };
        
        string json = JsonConvert.SerializeObject(product, Formatting.None);
        Console.WriteLine("Compact JSON:");
        Console.WriteLine(json);
        
        Console.WriteLine("\nJSON Length: " + json.Length + " characters");
    }
}

The output of the above code is −

Compact JSON:
{"Sizes":["Small","Medium","Large"],"Price":3.9900,"Expiry":"2008-12-28T00:00:00","Name":"Apple"}

JSON Length: 95 characters

Comparison Example

This example demonstrates both formatting options side by side −

using System;
using Newtonsoft.Json;

class Book {
    public string Title { get; set; }
    public string Author { get; set; }
    public int Pages { get; set; }
    public string[] Genres { get; set; }
}

class Program {
    static void Main(string[] args) {
        Book book = new Book {
            Title = "C# Programming",
            Author = "John Doe",
            Pages = 350,
            Genres = new[] { "Programming", "Technology", "Education" }
        };
        
        string compactJson = JsonConvert.SerializeObject(book, Formatting.None);
        string formattedJson = JsonConvert.SerializeObject(book, Formatting.Indented);
        
        Console.WriteLine("Compact JSON (" + compactJson.Length + " chars):");
        Console.WriteLine(compactJson);
        
        Console.WriteLine("\nFormatted JSON (" + formattedJson.Length + " chars):");
        Console.WriteLine(formattedJson);
    }
}

The output of the above code is −

Compact JSON (89 chars):
{"Title":"C# Programming","Author":"John Doe","Pages":350,"Genres":["Programming","Technology","Education"]}

Formatted JSON (130 chars):
{
  "Title": "C# Programming",
  "Author": "John Doe",
  "Pages": 350,
  "Genres": [
    "Programming",
    "Technology",
    "Education"
  ]
}

Conclusion

JSON formatting in C# using Newtonsoft.Json provides two main options: Formatting.None for compact, space-efficient JSON ideal for APIs and data transfer, and Formatting.Indented for human-readable JSON perfect for debugging and configuration files. Choose the appropriate formatting based on your specific use case and requirements.

Updated on: 2026-03-17T07:04:36+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements