Controlling JSON Encoding Output in PHP: UTF-8 vs. Unicode Escapes
When you run json_encode on an array containing non-ASCII characters, such as 'á', the default behavior is to escape them as \u00e1. This produces output like {"a":"\u00e1"}. While this is valid JSON and technically correct according to the specification, it often breaks frontend JavaScript rendering if your application expects raw UTF-8 bytes directly in the response body. Common attempts to fix this, such as setting mb_internal_encoding('UTF-8') or manually running utf8_encode on the array values, fail because json_encode does not respect the internal encoding for character escaping; it operates strictly on code points.
To achieve the expected result of {"a":"á"}âwhere the byte sequence represents the actual character rather than its Unicode escape sequenceâyou must explicitly control the encoding options passed to json_encode. The function provides a flag specifically for this purpose: the JSON_UNESCAPED_UNICODE constant. By passing this flag as an argument, you instruct PHP to output UTF-8 characters directly whenever possible, reserving escaping only for control characters (like newlines or tabs) that cannot be represented in JSON without backslashes.
Here is how you modify the existing code:
$arr = ['a' => 'á'];
// Without flag: Escapes non-ASCII characters
echo json_encode($arr);
// Output: {"a":"\u00e1"}
// With flag: Outputs raw UTF-8 characters
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
// Output: {"a":"á"}
This approach works because the JSON_UNESCAPED_UNICODE option tells the encoder to bypass Unicode escaping for characters in the BMP (Basic Multilingual Plane), which includes almost all common Latin, Greek, and Cyrillic characters. The function still encodes the output as UTF-8 by default, so the resulting byte stream is valid UTF-8 that can be interpreted correctly by modern browsers and JavaScript engines without further encoding steps like utf8_encode.
It is important to understand the trade-offs involved in using this flag. While it makes the JSON readable for humans viewing the raw response or debugging in a browser console, it removes some of the safety net provided by Unicode escapes. Specifically, characters that are valid in UTF-8 but might be misinterpreted if the receiving client assumes a different encoding (like ISO-8859-1) could cause issues if the client does not explicitly declare UTF-8 support in its headers. However, in modern web development where Content-Type: application/json; charset=utf-8 is standard, this flag is generally safe and preferred for readability.
There are scenarios where you might want to avoid this flag entirely. If your JSON payload contains high-surrogate characters (code points above U+FFFF), such as certain emoji or rare mathematical symbols, json_encode will still escape them unless combined with other flags, but the primary goal of avoiding \u00XX for standard accented letters is met with JSON_UNESCAPED_UNICODE. Additionally, if you are generating JSON for older parsers that strictly require escaped unicode, omitting this flag ensures maximum compatibility at the cost of readability.
Another consideration involves mixed content. If your array contains both ASCII strings and multi-byte characters, the flag applies uniformly. There is no way to selectively escape only specific values within a single json_encode call; the encoding strategy is global for the generated object. If you need different behaviors for different fields, you must structure your data or generate separate JSON fragments before merging them.
For developers using frameworks like Laravel, this behavior remains consistent regardless of the version, as long as the underlying PHP version supports the JSON_UNESCAPED_UNICODE constant (PHP 5.4+). You can apply this directly in your controllers or helper functions without needing to modify global settings like mb_internal_encoding, which is irrelevant to the encoding logic of the JSON encoder itself.
Ultimately, if your goal is to provide a human-readable JSON response that matches the visual representation of your data, passing JSON_UNESCAPED_UNICODE is the correct and standard solution. It ensures that the output contains actual UTF-8 bytes for characters like 'á', eliminating the need for downstream encoding conversions and preventing the display of escape sequences in client-side applications.
