0

Hi i have the following issue where in some instances the json_decode does not work and i get an empty array as follows.

// for test purpose set the inbound enc parameter
$_POST["enc"] = "eyJ0cmFuc2NyaXB0IjoiLSAgICAgICAgICBQYXN0ZWQgdGhlIHRleHQgaW50byBOb3RlcGFkIBMgbm8gc3BlY2lhbCBjaGFyYWN0ZXJzIiwiaWQiOjcwLCJpc0FjdGlvbmVkIjp0cnVlLCJ1c2VyX2lkIjoxLCJ0YXNrX3R5cGVfaWQiOjEsImFjY291bnRfaWQiOjIxLCJhY2NvdW50X25hbWUiOiJURVNUIiwiZXZlbnRfZGF0ZSI6bnVsbH0=";

$decoded = base64_decode($_POST["enc"]);
$ar = (array)json_decode($decoded);

echo "<pre>";
    print_r($decoded);
echo "</pre>";

echo "<pre>";
    print_r($ar);
echo "</pre>";

$decoded displays as a json string, but $ar is null.

Any help please will be appreciated in helping me with this issue.

Florent
  • 12,310
  • 10
  • 49
  • 58
cghrmauritius
  • 177
  • 1
  • 3
  • 15

3 Answers3

3

You didn't supply the second parameter in json_decode to return as an array, and not an object

// for test purpose set the inbound enc parameter
$_POST["enc"] = "eyJ0cmFuc2NyaXB0IjoiLSAgICAgICAgICBQYXN0ZWQgdGhlIHRleHQgaW50byBOb3RlcGFkIBMgbm8gc3BlY2lhbCBjaGFyYWN0ZXJzIiwiaWQiOjcwLCJpc0FjdGlvbmVkIjp0cnVlLCJ1c2VyX2lkIjoxLCJ0YXNrX3R5cGVfaWQiOjEsImFjY291bnRfaWQiOjIxLCJhY2NvdW50X25hbWUiOiJURVNUIiwiZXZlbnRfZGF0ZSI6bnVsbH0=";

$decoded = base64_decode($_POST["enc"]);
$ar = json_decode($decoded, true); //<-- Now returned as an array, and not an object

echo "<pre>";
    print_r($decoded);
echo "</pre>";

echo "<pre>";
    print_r($ar);
echo "</pre>";   

Output

{"transcript":"-          Pasted the text into Notepad  no special characters","id":70,"isActioned":true,"user_id":1,"task_type_id":1,"account_id":21,"account_name":"TEST","event_date":null}
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
  • ...because it doesn't work... it's valid to cast an object as an array. The error is with his raw `JSON`, not the `PHP`. – jterry Feb 03 '14 at 14:39
  • [Online JSON Parser](http://json.parser.online.fr/) & [JSONLint](http://jsonlint.com/) both say it's valid here. – ʰᵈˑ Feb 03 '14 at 14:41
  • Remove the `print_r($decoded)` and your script won't output anything, as `$ar` will still be `false`. – jterry Feb 03 '14 at 14:42
2

The error is in your JSON - oddly in the two spaces between "Notepad" and "no".

It looks like there's a nonstandard character between the spaces. Remove that character and the JSON is valid.

Invalid

{"transcript":"- Pasted the text into Notepad  no special characters","id":70,"isActioned":true,"user_id":1,"task_type_id":1,"account_id":21,"account_name":"TEST","event_date":null}

Valid

{"transcript":"- Pasted the text into Notepad  no special characters","id":70,"isActioned":true,"user_id":1,"task_type_id":1,"account_id":21,"account_name":"TEST","event_date":null}

In the future, ideally you would use json_encode to build your JSON string. The function will automatically escape any non-valid characters for you.

jterry
  • 6,209
  • 2
  • 30
  • 36
1

Taking your code and decoding the Base64 encoded string it turns out you have a CHR(13) ASCII character in your JSON data which is causing the JSON to fail validation according to JSON Lint. Taking that character out results in the JSON parsing correctly.

PHP Fiddle

Decoded JSON data:

{
    "transcript": "-          Pasted the text into Notepad  no special characters",
    "id": 70,
    "isActioned": true,
    "user_id": 1,
    "task_type_id": 1,
    "account_id": 21,
    "account_name": "TEST",
    "event_date": null
}

HEX Editor Screenshot:
HEX Editor Screenshot

AeroX
  • 3,387
  • 2
  • 25
  • 39
  • You are correct on the extra char the user copied and pasted from an email and it was a long minus sign, how can i ensure that all chars get handled correctly is really the main point here. – cghrmauritius Feb 03 '14 at 14:45
  • 1
    If you are able to correct the string before it is JSON encoded that would be the best way to fix the problem. Possibly a regular expression could remove non standard characters something like `preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string)` will remove all non-printable characters (regex from answer: http://stackoverflow.com/a/1176923/2594742) – AeroX Feb 03 '14 at 14:53