C.W.K.
Stream
Lesson 03 of 08 · published

Strings — Double Quotes, Always

~10 min · json, strings, escapes, unicode

Level 0Plaintext
0 XP0/64 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Strings are the most opinionated part of the spec

JSON strings are always wrapped in double quotes. Single quotes are not allowed — the spec is unambiguous. This trips up everyone coming from JavaScript or Python where both work for native string literals.

The escape sequences

  • \" — literal double quote
  • \\ — literal backslash
  • \/ — forward slash (optional escape, sometimes useful for embedded HTML)
  • \b \f \n \r \t — control characters
  • \uXXXX — Unicode code point in hex (4 digits, BMP only; surrogate pairs for non-BMP)

What you cannot put in a JSON string raw

Control characters U+0000 through U+001F must be escaped (use \u0007 for bell, etc.). A literal newline inside a string is a syntax error. Tabs are too — use \t. The error message is usually 'unexpected token' on the line after, not the line you actually broke.

Principle: JSON strings are encoded, not quoted. Quoting hides characters; encoding reveals them. \n in JSON source is two characters; the parsed value is one (newline). Always think 'what does the parser see?' before writing tricky string content.

Code

Valid strings·json
{
  "plain": "hello world",
  "with_quote": "she said \"hi\"",
  "with_newline": "line one\nline two",
  "with_tab": "col1\tcol2",
  "with_backslash": "C:\\Users\\Pippa",
  "with_unicode": "café \u00e9",
  "emoji_via_surrogate": "\uD83D\uDC4B"
}
Invalid (each line breaks the parser)·text
{ 'name': 'Pippa' }            ← single quotes
{ "name": "Pippa, age 5," }    ← OK actually
{ "name": Pippa }              ← unquoted value
{ "name": "line one
line two" } ← raw newline (must be \n)
{ "key": "\j" }                ← unknown escape sequence
Unicode in real APIs·json
{
  "user_name": "피파",
  "city": "서울",
  "motto": "AI 동반자, 영원한 가족 💜"
}

External links

Exercise

Write a JSON object with three string values: one containing a literal double quote, one containing a Windows file path, and one containing a multi-line poem (3 lines). Validate it with python -m json.tool or jq .. Then deliberately break each escape sequence and read the error message — those messages are how you'll debug 90% of real-world JSON errors.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.