03 Strings

A string is a piece of text in JSON. It can contain letters, numbers, symbols, or even be empty.

Every string must be written inside double quotation marks (").

Valid JSON Strings

Here are some valid string examples:

[
    "Hello",
    "Welcome to JSON",
    "12345",
    "",
    "😊"
]

Notice that all the values are inside double quotes.

  • "Hello" is a string.
  • "12345" is also a string because it is inside quotes.
  • "" is an empty string, which means it contains no characters.

Strings Must Use Double Quotes

JSON only allows double quotes for strings.

✅ Correct

{
    "city": "London"
}

❌ Wrong

{
    "city": 'London'
}

Why is it wrong? JSON does not allow single quotes (') for strings.

Escape Characters

Sometimes you need to include special characters inside a string. JSON uses a backslash (\) before these special characters. These are called escape characters.

1. Double Quote (\")

Use \" when you want to include double quotes inside a string.

{
    "message": "She said, \"Good morning!\""
}

2. Backslash (\\)

Use \\ to display a backslash.

{
    "path": "C:\\Users\\Alex"
}

3. Forward Slash (\/)

A forward slash usually does not need to be escaped, but JSON allows it.

{
    "website": "https:\/\/example.com"
}

4. New Line (\n)

Use \n to move text to the next line.

{
    "text": "Line 1\nLine 2"
}

5. Tab (\t)

Use \t to insert a tab space.

{
    "text": "Name\tAge"
}

6. Backspace (\b)

\b represents a backspace character. It removes the character before it when processed by some programs.

{
    "example": "ABC\bD"
}

7. Form Feed (\f)

\f represents a form feed, which is mainly used when printing documents or moving to a new page.

{
    "example": "Page1\fPage2"
}

8. Carriage Return (\r)

\r moves the cursor back to the beginning of the current line.

{
    "text": "Hello\rWorld"
}

9. Unicode Character (\u)

Use \u followed by four hexadecimal digits to represent a Unicode character.

{
    "letter": "\u0042"
}

Here, 0042 is the Unicode value for the letter B.

Summary of Escape Characters

Escape CharacterMeaningExample
\"Double quote"He said, \"Hi!\""
\\Backslash"C:\\Files"
\/Forward slash"https:\/\/example.com"
\nNew line"Hello\nWorld"
\tTab"Name\tAge"
\bBackspace"ABC\bD"
\fForm feed"Page1\fPage2"
\rCarriage return"Hello\rWorld"
\uXXXXUnicode character"\u0042" → B