Mastering JSON Encoding Options
While standard JSON.stringify() handles basic serialization, developers working across multiple backend frameworks (PHP, Python, Go, Java) often require granular control over character escaping. Forward slashes in URLs (https://example.com/api) and multibyte Unicode characters (such as accents, Japanese kanji, or Cyrillic) are frequently altered depending on serialization settings.
PHP json_encode() Flags Demystified
- JSON_PRETTY_PRINT: Uses whitespace formatting to indent levels with 4 spaces instead of minifying into a dense one-line payload.
- JSON_UNESCAPED_SLASHES: Prevents escaping the forward slash
/character into\/, which keeps URLs and directory paths clean and readable. - JSON_UNESCAPED_UNICODE: Encodes multibyte UTF-8 characters directly (e.g.
"café") instead of escaping them to hexadecimal escape sequences ("caf\u00e9"). - JSON_NUMERIC_CHECK: Evaluates numeric string values like
"123"or"45.67"and encodes them as true integer or float numbers.
Frequently Asked Questions
Can I input PHP array syntax into this encoder?
Yes! The input parser automatically recognizes PHP short array syntax (
['key' => 'value']) as well as standard JavaScript object literals without requiring manual conversion beforehand.Why does unescaping slashes save network bandwidth?
In payloads containing hundreds of image URLs or hyperlinks, each forward slash normally requires two bytes (
\/) instead of one (/). Enabling JSON_UNESCAPED_SLASHES reduces payload size by up to 15% on media-heavy endpoints.