1

Please check my below code and please let me know ehat wrong I am doing , I just want to decode the json and print on the browser as array

<?php
$json = '{
"Token" : "xxx-xxx-xxx",
"ID": "1",
"Recipients": [
 {
 "Recipient_ID": "XX",
 "From_Name": "XXX",
 "From_Email": "XXX",
 "To_Name": "XXX",
 "To_Email": "XXX",
 "Subject": "XXX",
 "Message": "XXX",
 "Attachments": [
 {
 "File_Name": "XXX",
 "File_Path": "XXX",
 }
 ],
 }
],
}';
$input = $json;
print_r(json_decode(stripslashes($input)));
?>

I tried this json string to decode online (http://jsonviewer.stack.hu/) and its working fine so no issue with json string. Any help would be appreciate.

asked Sep 1, 2016 at 13:27
7
  • 4
    You have 3 trailing , that you shouldn't have. Commented Sep 1, 2016 at 13:30
  • your json is also not valid look my updated answer Commented Sep 1, 2016 at 13:31
  • remove , at end of json string Commented Sep 1, 2016 at 13:33
  • @Jon Stirling : Its 2 trailing , Commented Sep 1, 2016 at 13:35
  • @RakeshSojitra It's 3. Commented Sep 1, 2016 at 13:36

2 Answers 2

2
<?php
$json = '{
 "Token": "xxx-xxx-xxx",
 "ID": "1",
 "Recipients": [{
 "Recipient_ID": "XX",
 "From_Name": "XXX",
 "From_Email": "XXX",
 "To_Name": "XXX",
 "To_Email": "XXX",
 "Subject": "XXX",
 "Message": "XXX",
 "Attachments": [{
 "File_Name": "XXX",
 "File_Path": "XXX"
 }]
 }]
}';
$input = $json;
var_dump(json_decode(stripslashes($input)));
?>
answered Sep 1, 2016 at 13:33
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please tell me what you changed from current one ?
you can copy the json I mentioned in the answer, I removed three extra commas
0

the problem stand from the json format you have.. You have some ',' that needs to be removed and that's why the json_decode() can't do his job. This function throws an error exception but you should do a little trick to view the error. You can use this code to view the error.

switch (json_last_error()) {
 case JSON_ERROR_NONE:
 echo ' - No errors';
 break;
 case JSON_ERROR_DEPTH:
 echo ' - Maximum stack depth exceeded';
 break;
 case JSON_ERROR_STATE_MISMATCH:
 echo ' - Underflow or the modes mismatch';
 break;
 case JSON_ERROR_CTRL_CHAR:
 echo ' - Unexpected control character found';
 break;
 case JSON_ERROR_SYNTAX:
 echo ' - Syntax error, malformed JSON';
 break;
 case JSON_ERROR_UTF8:
 echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
 break;
 default:
 echo ' - Unknown error';
 break;
}

And you json should be like this.

$json = '{
"Token" : "xxx-xxx-xxx",
"ID": "1",
"Recipients": [
 {
 "Recipient_ID": "XX",
 "From_Name": "XXX",
 "From_Email": "XXX",
 "To_Name": "XXX",
 "To_Email": "XXX",
 "Subject": "XXX",
 "Message": "XXX",
 "Attachments": [
 {
 "File_Name": "XXX",
 "File_Path": "XXX"
 }
 ]
 }
]
}';
answered Sep 1, 2016 at 13:43

1 Comment

Thanks for better explainations :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.