0
0
CsharpHow-ToBeginner · 2 min read

C# How to Convert JSON to Dictionary Easily

Use JsonSerializer.Deserialize<Dictionary<string, object>>(jsonString) from System.Text.Json to convert a JSON string to a dictionary in C#.
📋

Examples

Input{"name":"John","age":30}
Output{"name":"John","age":30}
Input{"city":"Paris","temperature":20,"isSunny":true}
Output{"city":"Paris","temperature":20,"isSunny":true}
Input{}
Output{}
🧠

How to Think About It

To convert JSON to a dictionary in C#, you read the JSON string and use a built-in method to turn it into a dictionary object. This method reads the JSON keys and values and stores them as key-value pairs in the dictionary.
📐

Algorithm

1
Get the JSON string input.
2
Use the JSON deserialization method to convert the string into a dictionary.
3
Return or use the resulting dictionary object.
💻

Code

csharp
using System;
using System.Collections.Generic;
using System.Text.Json;

class Program
{
    static void Main()
    {
        string jsonString = "{\"name\":\"John\",\"age\":30}";
        var dictionary = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonString);
        foreach (var kvp in dictionary)
        {
            Console.WriteLine($"{kvp.Key}: {kvp.Value}");
        }
    }
}
Output
name: John age: 30
🔍

Dry Run

Let's trace the example JSON '{"name":"John","age":30}' through the code

1

Input JSON string

{"name":"John","age":30}

2

Deserialize JSON to dictionary

Dictionary with keys 'name' and 'age', values 'John' and 30

3

Print dictionary contents

Output lines: 'name: John' and 'age: 30'

KeyValue
nameJohn
age30
💡

Why This Works

Step 1: Deserialize JSON string

The JsonSerializer.Deserialize method reads the JSON string and converts it into a dictionary object with keys and values.

Step 2: Dictionary stores key-value pairs

Each JSON key becomes a dictionary key, and the JSON value becomes the dictionary value.

Step 3: Use dictionary in code

You can access dictionary values by their keys just like any other dictionary in C#.

🔄

Alternative Approaches

Using Newtonsoft.Json (Json.NET)
csharp
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

class Program
{
    static void Main()
    {
        string jsonString = "{\"name\":\"John\",\"age\":30}";
        var dictionary = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
        foreach (var kvp in dictionary)
        {
            Console.WriteLine($"{kvp.Key}: {kvp.Value}");
        }
    }
}
Newtonsoft.Json is popular and flexible but requires adding a NuGet package.
Deserialize to Dictionary<string, string>
csharp
using System;
using System.Collections.Generic;
using System.Text.Json;

class Program
{
    static void Main()
    {
        string jsonString = "{\"name\":\"John\",\"age\":\"30\"}";
        var dictionary = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonString);
        foreach (var kvp in dictionary)
        {
            Console.WriteLine($"{kvp.Key}: {kvp.Value}");
        }
    }
}
Use this if all JSON values are strings; otherwise, use object type.

Complexity: O(n) time, O(n) space

Time Complexity

Deserialization reads each character in the JSON string once, so time grows linearly with input size.

Space Complexity

The dictionary stores all key-value pairs, so space grows linearly with the number of entries.

Which Approach is Fastest?

Using System.Text.Json is faster and uses less memory than Newtonsoft.Json, but Newtonsoft.Json offers more features.

ApproachTimeSpaceBest For
System.Text.JsonO(n)O(n)Built-in, fast, simple JSON parsing
Newtonsoft.JsonO(n)O(n)Complex JSON, more features, legacy support
DictionaryO(n)O(n)Simple JSON with string values only
💡
Use System.Text.Json for built-in JSON support without extra packages.
⚠️
Trying to deserialize JSON with mixed value types into Dictionary<string, string> causes errors if values are not strings.