How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If the incoming JSON is a flat object with string values, deserializing to Dictionary<string, string> in ASP.NET is straightforward. The main complication is that real clients often send numbers, booleans, or nested objects, which no longer match a strict string-to-string dictionary.
So the real first step is to define the expected JSON shape. If the payload is truly key-value string metadata, a dictionary is fine. If the data has structure, a typed model is usually better.
The JSON Shape Must Match
A direct Dictionary<string, string> mapping expects JSON like this:
That shape is simple: one object, string keys, string values.
If the values are numbers or nested objects, direct deserialization may fail or may require custom conversion logic.
System.Text.Json Example
In modern .NET, System.Text.Json is the usual default serializer.
That is enough when the JSON is already a flat string map.
ASP.NET Controller Model Binding
In ASP.NET Core, the framework can bind the request body directly to a dictionary parameter.
This is concise and works well when the set of keys is intentionally dynamic.
Handling Non-String Values Explicitly
If clients may send values that are not strings, you need to decide on a policy. One option is to parse the JSON manually and convert all values to strings deliberately.
This makes the conversion rule explicit instead of relying on whatever a serializer might do by default.
When a Dictionary Is the Wrong Model
A dictionary is fine for metadata, dynamic form values, or user-defined key-value settings. It is a poor fit when the payload has a fixed domain shape.
For example, this is better modeled as a class than a dictionary:
If the keys are known in advance, a typed model gives you validation, clearer documentation, and better error messages.
Common Pitfalls
A common mistake is trying to deserialize nested JSON directly into Dictionary<string, string>. The shape simply does not match.
Another issue is assuming all clients will send strings because the server wants strings. Real clients often send booleans and numbers naturally.
Developers also sometimes use a dictionary for payloads that really deserve a structured DTO. That throws away schema clarity for no real benefit.
Finally, dynamic payloads still need validation. A dictionary is easy to deserialize, but unsafe keys and unexpected values can still create downstream problems.
Summary
- A flat JSON object with string values maps cleanly to
Dictionary<string, string>. - In ASP.NET, controller model binding can accept that dictionary directly.
- If values may be non-strings, convert them explicitly or switch to a typed model.
- Use dictionaries only when the key set is intentionally dynamic.
- If the payload has a stable schema, a dedicated DTO is usually the better design.
Related reading
- How can I determine if a .NET assembly was built for x86 or x64?
- How can I disable horizontal scrolling in a WPF ListBox?
- How can I force .NET to use a local copy of an assembly that's in the GAC
- How can I generate truly not pseudo random numbers with C?
- How can I generate UUID in C
- How can I get all constants of a type by reflection?
- How can I get Copy to Output Directory to work with Unit Tests?
- How can I get my webapp's base URL in ASP.NET MVC?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.