dotnet core
System.Text.Json
unicode
string unescape
C# serialization

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.

Browse interview questions

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:

csharp
1using System;
2using System.Text.Json;
3
4string json = "\"\\u4F60\\u597D\"";
5string value = JsonSerializer.Deserialize<string>(json)!;
6
7Console.WriteLine(value);

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:

csharp
1using System;
2using System.Text.Json;
3
4record Message(string Text);
5
6string json = "{\"Text\":\"\\u4F60\\u597D\"}";
7Message message = JsonSerializer.Deserialize<Message>(json)!;
8
9Console.WriteLine(message.Text);

JsonDocument also returns decoded strings

If you are navigating JSON manually, JsonDocument handles escapes too:

csharp
1using System;
2using System.Text.Json;
3
4string json = "{\"emoji\":\"\\uD83D\\uDE0A\"}";
5
6using JsonDocument doc = JsonDocument.Parse(json);
7string value = doc.RootElement.GetProperty("emoji").GetString()!;
8
9Console.WriteLine(value);

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:

csharp
string text = "\\u4F60\\u597D";

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:

csharp
1using System;
2using System.Text.Json;
3
4string raw = "\\u4F60\\u597D";
5string json = $"\"{raw}\"";
6string decoded = JsonSerializer.Deserialize<string>(json)!;
7
8Console.WriteLine(decoded);

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:

csharp
1using System;
2using System.Text.Encodings.Web;
3using System.Text.Json;
4
5var options = new JsonSerializerOptions
6{
7    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
8};
9
10string json = JsonSerializer.Serialize("你好", options);
11Console.WriteLine(json);

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.Json automatically decodes Unicode escapes when it parses valid JSON.'
  • 'JsonSerializer.Deserialize<string> and JsonDocument.GetString() both return decoded .NET strings.'
  • A plain C# string containing \uXXXX text 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.