Skip to main content

How to Write a Character as a Unicode Escape in a JSON String

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A \u escape, or Unicode escape, writes a character in a JSON string by its number: a backslash, the letter u and four hexadecimal digits. "caf\u00e9" is the word café, and \u0022 is a double quote.

This guide writes three characters as escapes in a small file and checks them with jq 1.8.2, along with the mistakes that break an escape or quietly change it.

Syntax​

General form
"\u<four hex digits>"
PartWhat it means
\A backslash starts the escape
uA lowercase u: the character's number comes next
<four hex digits>The character's number, its code point, in hexadecimal: exactly four of 0 to 9 and a to f

The JSON standard, RFC 8259, calls the backslash a reverse solidus: Any character may be escaped. If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), then it may be represented as a six-character sequence: a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point.

The Example​

A file called sign.json holds three strings: the word café with its é written as \u00e9, the word hi between two double quotes written as \u0022, and a price with the euro sign, \u20ac.

Prerequisites​

  • jq, to check the file. jq --version prints the version you have; this guide uses jq 1.8.2 (Download jq).

Step 1: Write the Escapes​

Create sign.json with this text:

sign.json
{
"word": "caf\u00e9",
"quote": "\u0022hi\u0022",
"price": "\u20ac5"
}

macOS-style window titled sign.json showing a JSON object whose three strings use escapes: caf followed by backslash u00e9, backslash u0022 on both sides of hi, and backslash u20ac before 5.

00e9 is the number of é, 0022 the number of the double quote, and 20ac the number of the euro sign. A double quote can't sit inside a string as it is, because it would end the string. The RFC lists the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters.

Step 2: Check It with jq​

Terminal
jq . sign.json
Output
{
"word": "café",
"quote": "\"hi\"",
"price": "€5"
}

jq read each escape as the character it stands for. It printed é and € as themselves, and wrote the double quotes back in their short form, \". The jq manual: jq usually outputs non-ASCII Unicode codepoints as UTF-8, even if the input specified them as escape sequences (like "\u03bc"). The RFC allows both forms of the quote: Alternatively, there are two-character sequence escape representations of some popular characters.

Step 3: Print the Values as Plain Text​

Terminal
jq -r '.quote' sign.json
jq -r '.word' sign.json
Output
"hi"
café

-r prints a string without the JSON quotes around it. The manual: With this option, if the filter's result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. So the quotes around hi are the two \u0022 escapes, now real double quotes.

Step 4: Let jq Write the Escapes​

Terminal
jq -a . sign.json
Output
{
"word": "caf\u00e9",
"quote": "\"hi\"",
"price": "\u20ac5"
}

macOS-style terminal window titled Terminal showing a real zsh session: jq . sign.json prints café, the quoted hi and €5, jq -r prints the quote and the word as plain text, and jq -a writes the é and the euro sign back as escapes.

-a turns every character outside ASCII back into an escape. The manual: Using this option, you can force jq to produce pure ASCII output with every non-ASCII character replaced with the equivalent escape sequence. é and € came back as the same escapes as in the file.

Characters Beyond Four Hex Digits​

Four digits go up to ffff. A character with a bigger number takes two escapes in a row, called a surrogate pair. The RFC's own example is the musical G clef, U+1D11E: To escape an extended character that is not in the Basic Multilingual Plane, the character is represented as a 12-character sequence, encoding the UTF-16 surrogate pair. So, for example, a string containing only the G clef character (U+1D11E) may be represented as "\uD834\uDD1E".

clef.json
{
"clef": "\uD834\uDD1E"
}
Terminal
jq . clef.json
jq -a . clef.json
Output
{
"clef": "𝄞"
}
{
"clef": "\ud834\udd1e"
}

jq turned the pair into one character, 𝄞, and -a wrote the same pair back with lowercase letters.

Common Mistakes​

MistakeWhat happensDo this instead
Three digits: "caf\u0e9"jq: parse error: Invalid \uXXXX escape at line 2, column 20Write all four: \u00e9
A capital U: "caf\U00e9"jq: parse error: Invalid escape at line 2, column 21Use a lowercase u
Five digits for the G clef: "\u1D11E"No error, but the wrong text: jq printed ᴑEUse the surrogate pair, \uD834\uDD1E

The last one is the hardest to spot. An escape always takes exactly four digits, so jq read \u1D11, the character ᴑ, followed by an ordinary letter E.

FAQ​

Do the letters in the number have to be lowercase?​

No. case.json has "caf\u00e9" in one string and "caf\u00E9" in another, and jq '.lower == .upper' case.json printed true. The RFC: The hexadecimal letters A through F can be uppercase or lowercase.

How do I find a character's escape?​

Let jq write it. jq -n -a '"é"' printed "\u00e9", and jq -n -a '"€"' printed "\u20ac". -n runs jq without an input file. The manual: Don't read any input at all.

Conclusion​

In sign.json, "caf\u00e9", "\u0022hi\u0022" and "\u20ac5" are café, "hi" in quotes, and €5. jq . printed the characters, and jq -a wrote them back as escapes. Use a lowercase u with exactly four hex digits, and a surrogate pair for anything above ffff.

Additional Resources​