I have a gzipped base64 JSON string, after converting this string json_decode() can’t decode it. I think it is an encoding issue, but have been unable to resolve it.
I am running PHP 5.2 which doesn’t support json_last_error().
Does anyone know what is going on here?
<?php
$gzipped_base64 = "7b0HYBxJliUmL23Ke39K9UrX4HShCIBgEyTYkEAQ7MGIzeaS7B1pRyMpqyqBymVWZV1mFkDM7Z28995777333nvvvfe6O51OJ/ff/z9cZmQBbPbOStrJniGAqsgfP358Hz8ifvGv8dGv8frXaH+N7NeY/hpvf40vf43LXyP/Nepf4/zXKH+N6te4ou8e0f9f0e/4tqHff8mv8f8A";
$json = gzinflate(base64_decode($gzipped_base64));
echo $json; // prints: {"StackOverflow":"Rocks"}
$array = json_decode($json, true);
echo var_dump($array); // prints: NULL
?>
json_decodeexpects the input to be UTF-8 encoded but your data seems to be encoded with UTF-16:The output is:
So convert the character encoding using
mb_convert_encodingoriconvto convert to UTF-8 before callingjson_decode.