Bootsector
Bootsector

Reputation: 35

PHP json_decode failed when value contains single quote (')

I cannot make json_decode() to work when the string value contains single quote (') as example below:

$result = "{\"message\":\"test \' \",\"report\":[{\"1\":[{\"port\":\"gsm-1.2\",\"phonenumber\":\"XXXXXXXXXXX\",\"time\":\"2016-08-31 00:22:57\",\"result\":\"success\"}]}]}";
$resp = json_decode($result, true);
echo $resp;

Upvotes: 2

Views: 4007

Answers (2)

Abhijit Jagtap
Abhijit Jagtap

Reputation: 2702

Your $result json is not in proper format so i think you need to use stripslashes() to format it and after use json_decode(). it would work :).

<?php
$result = "{\"message\":\"test \'\",\"report\":[{\"1\":[{\"port\":\"gsm-1.2\",\"phonenumber\":\"XXXXXXXXXXX\",\"time\":\"2016-08-  31 00:22:57\",\"result\":\"success\"}]}]}";
$result=stripslashes($result);
$resp = json_decode($result, true);
var_dump($resp);
?>

check on phpfiddle => http://phpfiddle.org/main/code/4e7n-vjxa

Upvotes: 2

Vivek Srivastava
Vivek Srivastava

Reputation: 569

In your code the single quote(') is skipped with slash () thus it is breaking the JSON format.

Try removing the slash and try. It should work.

You should check the code where you are generating this JSON.

Upvotes: 1

Related Questions