0
0
CsharpHow-ToBeginner · 3 min read

How to Read JSON File in C# - Simple Guide

To read a JSON file in C#, use File.ReadAllText to load the file content as a string, then parse it with JsonSerializer.Deserialize<T> from System.Text.Json. This converts the JSON text into a C# object you can work with.
📐

Syntax

Here is the basic syntax to read and parse a JSON file in C#:

  • File.ReadAllText(path): Reads the entire JSON file content as a string.
  • JsonSerializer.Deserialize<T>(jsonString): Converts the JSON string into an object of type T.
csharp
string jsonString = File.ReadAllText("path/to/file.json");
var obj = JsonSerializer.Deserialize<T>(jsonString);
💻

Example

This example shows how to read a JSON file containing a simple person object and print its properties.

csharp
using System;
using System.IO;
using System.Text.Json;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        string filePath = "person.json";
        string jsonString = File.ReadAllText(filePath);
        Person person = JsonSerializer.Deserialize<Person>(jsonString);

        Console.WriteLine($"Name: {person.Name}");
        Console.WriteLine($"Age: {person.Age}");
    }
}
Output
Name: Alice Age: 30
⚠️

Common Pitfalls

Common mistakes when reading JSON files in C# include:

  • Not matching the JSON structure with the C# class properties exactly (case-sensitive by default).
  • Forgetting to include using System.Text.Json; or missing the System.Text.Json package.
  • Trying to deserialize to the wrong type or a type that does not match the JSON format.
  • Not handling exceptions when the file path is wrong or JSON is invalid.

Always ensure your C# class matches the JSON keys and handle exceptions for robust code.

csharp
/* Wrong way: Property names do not match JSON keys (case-sensitive) */
public class Person
{
    public string name { get; set; } // should be 'Name'
    public int age { get; set; }     // should be 'Age'
}

/* Right way: Match property names exactly or use JsonPropertyName attribute */
using System.Text.Json.Serialization;
public class Person
{
    [JsonPropertyName("name")]
    public string Name { get; set; }
    [JsonPropertyName("age")]
    public int Age { get; set; }
}
📊

Quick Reference

Summary tips for reading JSON files in C#:

  • Use File.ReadAllText to read the file content.
  • Use JsonSerializer.Deserialize<T> to convert JSON string to object.
  • Ensure your C# class matches JSON structure and property names.
  • Handle exceptions for file access and JSON parsing errors.
  • Include using System.Text.Json; and target .NET Core 3.0+ or .NET 5+.

Key Takeaways

Use File.ReadAllText to load JSON file content as a string.
Deserialize JSON string to a C# object with JsonSerializer.Deserialize.
Match C# class properties exactly to JSON keys or use JsonPropertyName attribute.
Handle exceptions for missing files or invalid JSON to avoid crashes.
System.Text.Json is the modern, built-in library for JSON in C# (.NET Core 3.0+).