json_decode and json_encode: number and Unicode edge cases
Most JSON code in PHP is two function calls. The surprises come from data that is slightly unusual: an ID from another system, a price, a name with a stray byte.
Big integers
Integers larger than PHP_INT_MAX (9,223,372,036,854,775,807 on a 64-bit build) are converted to floats by json_decode, and lose precision. A 20-digit identifier from a payment or blockchain API comes back rounded. Pass JSON_BIGINT_AS_STRING to receive such numbers as strings:
$data = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
Only numbers that overflow are affected; small integers stay integers. When you control the API, send large identifiers and amounts as strings in the first place, since the same problem exists in JavaScript clients, which lose precision above 2^53.
Floats
json_encode uses the serialize_precision INI setting for floats. Since PHP 7.1 the default is -1, which selects the shortest representation that reads back to the same value. The output for 0.1 + 0.2 is 0.30000000000000004, which is the honest value of the sum. If you see 14-digit or 17-digit oddities in older configuration files, check that serialize_precision is -1 and not left at a value inherited from an old php.ini.
For money, do not encode floats at all. Use strings or integers in minor units, and do the arithmetic with bcmath or a decimal library.
A related detail: a float with no fractional part is encoded as an integer, so 10.0 becomes 10. If the consumer cares about the type, JSON_PRESERVE_ZERO_FRACTION keeps it as 10.0.
Errors
By default json_decode returns null on invalid JSON, which is indistinguishable from a valid null, and you have to call json_last_error(). Use JSON_THROW_ON_ERROR (PHP 7.3 and later) to get a JsonException instead:

The flag also applies to json_encode, which otherwise returns false on failure.
Invalid UTF-8
JSON must be valid UTF-8. If a string contains an invalid byte sequence, for example text read from a legacy database or a Latin-1 file, json_encode fails, and without the throw flag you get false and an empty response body. Fix the source by converting the data to UTF-8 (mb_convert_encoding). As a safety net, JSON_INVALID_UTF8_SUBSTITUTE replaces bad bytes with U+FFFD and JSON_INVALID_UTF8_IGNORE drops them. Both hide data corruption, so log when they trigger.
Output details
JSON_UNESCAPED_UNICODEandJSON_UNESCAPED_SLASHESproduce more readable output, at the cost of a slightly larger risk when embedding JSON in HTML. If you print JSON into a script tag, escape it properly (JSON_HEX_TAG,JSON_HEX_AMPand the related flags).- An empty PHP array encodes as
[]even when you wanted an object. Cast to(object)or useJSON_FORCE_OBJECTwhere the distinction matters. - Associative arrays whose keys are not sequential from zero encode as objects, so array-versus-object output can change with the data.
- The depth limit defaults to 512. Deeply nested untrusted input can be limited by passing a smaller depth.
JSON_NUMERIC_CHECK quietly reinterprets strings you meant to keep as strings
json_encode's JSON_NUMERIC_CHECK flag converts any string that looks numeric into an actual JSON number in the output, which sounds convenient and is a common source of a specific, hard-to-notice data bug: an identifier, phone extension or code stored as the string "007" becomes the number 7 once encoded with the flag, silently dropping the leading zero, and a string like "1.50" representing a fixed two-decimal price becomes 1.5, losing the trailing zero a display layer may have depended on. The flag looks at the string's content, not at why your application chose to store it as a string in the first place, so anything that is a string specifically to preserve formatting (leading zeros, fixed decimal places, a value too large to be a safe float, exactly the big-integer case above) is exactly what it breaks. Leave it off unless you have a specific field where numeric strings really should become numbers, and reach for explicit casting on that one field instead.
For diagnosing decode failures during development, json_last_error_msg() returns a human-readable description ("Syntax error", "Malformed UTF-8 characters") alongside the machine-readable code from json_last_error(), which is usually enough to spot the problem without reaching for a JSON linter separately.