problems with german umlauts in php json_encode

problems with german umlauts in php json_encode

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Problems with German Umlauts in PHP json_encode

When handling data from a database containing special characters like German umlauts (ä, ö, ü, ß), expecting them to appear correctly is only half the battle. The other half involves ensuring that serialization and deserialization pipelines preserve these bytes intact. A common scenario arises when fetching rows where you must execute SET NAMES utf8 before querying MySQL to ensure the connection interprets incoming bytes correctly. Once that step is handled, the data often arrives in your PHP variable as a valid UTF-8 string.

However, issues frequently emerge at the serialization stage. If you observe that umlauts turn into black square question marks (‍) when viewing a page directly but disappear entirely—becoming null—when using json_encode() on the same result set, the root cause usually lies in how JSON encoding handles control characters and specific byte sequences.

While it is tempting to strip or replace these characters before encoding to force compatibility with older JavaScript parsers, that approach sacrifices data integrity. Modern standards support UTF-8 natively, so replacing valid characters is a workaround that should be avoided unless you have strict legacy constraints.

The json_encode Behavior

In standard PHP configurations, json_encode() adheres strictly to the RFC 7159 specification. This means it does not perform character substitution; instead, it encodes non-ASCII characters using Unicode escape sequences within the JSON string. Specifically, an umlaut like 'ä' will be encoded as \u00e4 rather than appearing as the glyph itself.

If your application relies on a browser that renders these escape sequences directly or if your JavaScript consumer expects raw UTF-8 bytes in the response body without decoding them first, you might encounter rendering issues. However, the specific symptom of data turning into null suggests a more subtle problem: null characters (\0) or control characters within the string causing the encoder to truncate the output or fail silently depending on the PHP version and encoding settings.

Handling Control Characters

Older versions of PHP or specific configurations might treat certain byte sequences as invalid UTF-8 or as control characters that need escaping. When json_encode encounters a string with a leading null character or specific high-bit artifacts, it can sometimes result in unexpected truncation or the value being treated as empty/null in certain contexts.

If you are seeing null instead of an escaped string like "value": "\u00e4", check your input data for invisible characters introduced by the database layer or binary blobs that were not properly cast to strings. Ensure that your column types are defined as VARCHAR with a UTF-8 charset and collation, not TEXT with mixed encoding, which can sometimes lead to partial reads returning malformed strings.

The Proper Solution: UTF-8 Everywhere

The most robust solution is to ensure the entire stack operates on valid UTF-8 without manual character replacement. This involves:

  1. Database Layer: Keep the connection charset set to utf8mb4 (preferable over legacy utf8) and ensure columns are defined with matching charsets.
  2. PHP Layer: Configure your PHP environment to handle strings as UTF-8 internally, which is default in modern versions, but verify via mb_internal_encoding('UTF-8') if necessary.
  3. JSON Output: Allow json_encode() to output Unicode escapes. Modern JavaScript engines (V8, SpiderMonkey) decode \uXXXX sequences perfectly well into the original glyphs upon parsing.

Implementation Example

Here is a robust pattern for serializing data that includes umlauts without manual replacement:

// Assume $row contains data fetched after 'SET NAMES utf8' or equivalent connection setup
$data = [
    'name' => 'Müller',
    'city' => 'München',
    'product' => 'Öl'
];

$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);

Using JSON_UNESCAPED_UNICODE allows the umlauts to appear as actual characters in the JSON string ("name": "Müller"). If your target environment is modern (PHP 7.4+, Node.js, recent browsers), this produces valid UTF-8 JSON that renders correctly.

If you must stick to raw escapes for compatibility with very old parsers, omit JSON_UNESCAPED_UNICODE. In no standard scenario should you use str_replace() on the original data before encoding, as this introduces logic errors and potential security risks if the replacement strings are not carefully managed.

Conclusion

The issue of umlauts appearing as black squares or null values is almost always a mismatch between client-side rendering expectations and server-side serialization settings, rather than a flaw in PHP's JSON encoder itself. By configuring your database connections correctly and leveraging json_encode()'s built-in Unicode support, you can maintain data integrity without resorting to character replacement.

Our network