json_decode() fails on json string with more byte length than visible characters
It is a common source of frustration when json_decode() appears to behave inconsistently. You might have a variable that looks perfectly valid, matches the exact character count of a manually typed string, and yet PHP refuses to parse it while accepting the manual copy-paste version. The specific scenario where this breaks down often involves data retrieved from encrypted payloads processed through $_GET parameters.
The root cause here is almost never a bug in the JSON parser itself. It is an encoding mismatch between what you see and what PHP actually receives. When you manually copy a string into your code, your text editor or clipboard handles the character conversion automatically. However, when PHP reads $_GET['e'] from the browser, it has already performed URL decoding on that input. If the data was encrypted and then double-encoded (Base64 and URL encoded) before hitting the server, the decoding process can introduce subtle byte-level changes that standard string length checks miss.
Consider the specific example provided where var_dump($jsonData) reports a string length of 144 bytes, yet a manual count of visible characters yields only 124. This discrepancy is the smoking gun. In a standard UTF-8 environment, this usually indicates the presence of invisible control characters or non-breaking spaces that were introduced during the URL encoding/decoding cycle or by the specific PHP version handling the input stream.
The code snippet using mcrypt_encrypt with Blowfish in ECB mode is functionally sound for encryption, but the post-processing steps are where the data integrity risks lie:
$iv = rawurlencode(base64_encode($iv));
$enc = rawurlencode(base64_encode($enc));
// To Decrypt
$iv = base64_decode(rawurldecode($_GET['i']));
$enc = base64_decode(rawurldecode($_GET['e']));
When you decode the URL and Base64 strings, you expect to get back the raw ciphertext. However, if the original JSON contained certain characters (like quotes or forward slashes), the rawurlencode function might have treated them differently depending on the server's configuration or PHP version. More critically, if the decrypted data contains bytes that represent control characters (like \x00 or non-breaking spaces) rather than standard printable ASCII, json_decode() will silently fail and return null.
You can verify this hypothesis by checking the actual byte representation of the string using bin2hex():
var_dump(bin2hex($jsonData));
If you see a pattern that does not match the hex equivalent of the manually typed string, it confirms the data has been corrupted or altered during transmission. The manual copy-paste works because your editor strips invisible characters or normalizes them when you paste into the source code directly.
The most robust fix is to ensure strict validation before attempting to decode. You should check if json_decode() returns null and inspect the error state using json_last_error_msg(). Furthermore, if you are working with data coming from a client-side script that sends JSON via GET parameters, consider switching to POST requests or using URL-safe Base64 variants (like Base64url) which do not require the extra rawurlencode() wrapping step for standard characters.
If you must continue using this architecture, ensure that your decryption logic strictly validates the output of mcrypt_decrypt before passing it to json_decode. A simple check involves ensuring the resulting string only contains valid JSON characters and no trailing whitespace or control codes:
$decoded = json_decode($jsonData, TRUE);
if ($decoded === null) {
$error = json_last_error();
// Handle error: Check for JSON_ERROR_SYNTAX or similar
}
For modern PHP applications, relying on legacy mcrypt extensions is often discouraged in favor of the libsodium extension, which handles these encoding nuances more transparently. However, if you are locked into this older stack, rigorous byte-level inspection is your only defense against invisible encoding bugs. The issue isn't that json_decode() is failing arbitrarily; it is that the input stream contains bytes that do not represent a valid JSON string in the context of the current PHP environment.
