Characters That Must Be Escaped
The JSON specification requires escaping for: double quote (\"), backslash (\\), and control characters (\n newline, \r carriage return, \t tab, \b backspace, \f form feed). Any attempt to include them unescaped will cause a parse error or data corruption.
Unicode Escaping
Any Unicode character can be escaped as \uXXXX where XXXX is the Unicode code point in hexadecimal. In practice, modern JSON parsers handle most Unicode directly in UTF-8 encoded JSON, making \u sequences necessary only for control characters or when producing ASCII-safe JSON.
Automatic Escaping in Libraries
JSON serialization libraries handle escaping automatically — you never need to manually escape values before serializing. The danger is in string concatenation: manually building JSON by concatenating strings will not escape values and can produce invalid JSON. Always use serialization libraries.
Debugging Escape Issues
Escaping bugs typically manifest as 'unexpected token' parse errors at a specific position. Common culprits: smart quotes instead of straight quotes, non-breaking spaces, and Windows-style line endings in multiline string values.
Let serialization libraries handle JSON escaping automatically. Manual JSON construction through string concatenation is always wrong. When debugging parse errors, inspect the raw bytes of the problematic string.