Character encoding problems explained
November 17, 2026 · 14 min read
Encoding bugs look like “the API broke” when the real issue is bytes interpreted with the wrong charset. Mojibake - garbled accents and symbols - happens when UTF-8 bytes are read as Latin-1 or Windows-1252. Modern stacks should default to UTF-8 end to end and only transcode at legacy boundaries.
UTF-8 as the default
UTF-8 encodes Unicode code points variably; ASCII stays one byte. JSON (RFC 8259) requires UTF-8 encoding. Node, Go, and Java string types are UTF-16 internally but IO should be UTF-8 for HTTP and files.
Mojibake and double encoding
Classic symptom: café instead of café. Fix by identifying the misread path - often a CSV opened in Excel as ANSI or a database column still labeled latin1. Re-import from source with correct charset rather than guessing replacements.
- Double UTF-8 encoding - decode once, detect invalid sequences early.
- Emoji corruption - surrogate mishandling in older Java APIs; use modern charset APIs.
- BOM - UTF-8 BOM is optional; strip on ingest for APIs.
HTTP Content-Type and charset
Use Content-Type: application/json; charset=utf-8 (charset optional when UTF-8 per spec). HTML needs <meta charset="utf-8"> or header. Mismatched declaration makes browsers render wrong bytes even if the file is UTF-8.
Files, databases, and logs
Set database client encoding to UTF8. Log appenders should write UTF-8. When legacy systems emit Latin-1, convert explicitly at the integration point and document the contract.
When Base64 helps
Binary data in JSON or email must not be raw bytes - use Base64 in a string field. Convert text↔bytes carefully with the Base64 converter when debugging whether corruption happened before or after encoding.
FAQ
- Is UTF-8 the same as Unicode?
- Unicode is the character set; UTF-8 is one encoding of it. UTF-16 is another common in-memory form.
- Why does JSON reject some bytes?
- JSON must be valid Unicode; unescaped control characters are not allowed in strings.
- Should I use charset=utf-8 in every response?
- Explicit charset helps older clients; UTF-8 is default for JSON in modern stacks.
Related: UTF-8 vs Base64 · Common encoding problems in APIs · What is Base64 encoding · Why JWT uses Base64URL