dotnet core System.Text.Json unescape unicode string
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you have a JSON string containing Unicode escape sequences such as \u4F60\u597D, System.Text.Json will decode them when it parses JSON correctly. The most important distinction is whether your input is actual JSON text or just an ordinary C# string that happens to contain backslash characters. System.Text.Json is a JSON parser, not a general-purpose string-unescape utility.
Deserializing a JSON string already unescapes it
If the input is a JSON string literal, deserialization is enough:
That prints the decoded Unicode text. In other words, you usually do not need a separate "unescape" API if you are already parsing JSON properly.
The same applies when the string lives inside a JSON object:
JsonDocument also returns decoded strings
If you are navigating JSON manually, JsonDocument handles escapes too:
GetString() gives you the decoded .NET string value, not the raw JSON escape sequence.
If the input is not JSON, parsing rules are different
This is where people get confused. Suppose you have a plain C# string like this:
That is not valid JSON by itself. It is just a sequence of characters that looks like part of a JSON string body. JsonSerializer.Deserialize<string>(text) will fail because the parser expects quoted JSON text.
If you want to use System.Text.Json to decode it, wrap it as a JSON string first:
That works because you turned the raw escaped content into valid JSON.
Do not hand-roll Unicode replacements
A common temptation is to write custom code that searches for \uXXXX patterns and replaces them manually. That is usually unnecessary and often incomplete, especially once surrogate pairs such as emoji enter the picture. If the data is JSON, let the JSON parser do the decoding. If the data is not JSON, normalize it into valid JSON or use a dedicated text-processing approach instead of mixing the two responsibilities.
Serialization is the opposite direction
Sometimes the real issue is not decoding on input, but preventing unnecessary escaping on output. By default, System.Text.Json may escape some characters when serializing. If you want more natural-looking Unicode output, configure the encoder:
That affects serialization behavior, not deserialization. It is a separate concern, but the two often get mixed together.
Common Pitfalls
The most common mistake is expecting System.Text.Json to unescape arbitrary strings that are not valid JSON. It parses JSON, not free-form text.
Another common issue is manually trying to replace \uXXXX sequences when deserialization would already decode them correctly.
People also confuse input decoding with output escaping. Deserializing Unicode and configuring serializer escaping are related but different tasks.
Finally, if the input is only a JSON fragment rather than a full JSON string literal, wrap or parse it appropriately before expecting System.Text.Json to decode it.
Summary
- '
System.Text.Jsonautomatically decodes Unicode escapes when it parses valid JSON.' - '
JsonSerializer.Deserialize<string>andJsonDocument.GetString()both return decoded .NET strings.' - A plain C# string containing
\uXXXXtext is not the same thing as a JSON string literal. - If needed, wrap raw escaped text in quotes so it becomes valid JSON before deserializing.
- Output escaping during serialization is a separate concern from input decoding.
Related reading
- ''dotnet restore'' vs. ''nuget restore'' with TeamCity
- dotnet restore warning NU1701
- double? double? double?
- Double.TryParse or Convert.ToDouble - which is faster and safer?
- Download file with WebClient or HttpClient?
- Download old version of package with NuGet
- DownloadStringAsync wait for request completion
- Drag and drop files into WPF

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.