0
$foo_json = '{"hoge":"ywwxEu|`tYdBeARGJ?nJ~BHHmDjX|PdEl@Rj@\\XVtKtK\\VbKnFrIbEzEbBnEbBfDfAxBz@LJjBl@jEzAfZzKDPY|DUdCGbAFL?|AD~AJ\\LNPJ~B|@p@Ll@FRDd@RvBnAfA`@dAl@^NhAVd@@p@?h@H`@Pv@l@TR^f@`Rd@xAFdEVnCLjJn@Cn@FlRDh@HRHHjKpG\\\\jC`G|EvJ|BjGf@vAJh@nGpC`FrBbG`D~@XdBRnNtAnTdBf@Jn@Zf@h@pB`DrD|EZl@jBfEf@`Af@r@tCpChFrG~@zAx@dBdB~Cz@xA`AxA|@l@f@VdDrAnAl@jI~CpCv@|@f@~An@jIxD~CdAzAl@rCvAjI|CzJpClJbCvLdDbn@lH`d@pF~Df@dDn@hA`@hBfA~AbAzQpMvHhFhQ~L~G`FtBrArCnBbBnAjB`A`~@~^tBx@n@RfB\\b@Fh@B|BBpV{AfSgArJq@~@?dBF`Cd@nFjAhXdG~@VtMpEd[fKfL|DxWrI~Y`KvLtDp\\zK|DnAzBbAbA|@rAtApAfBhA`Cl@fBh@lBtEpQh@fBrAjFdDpLdArCj@fAd@x@`BvBlDnDrVxWjDzD`GlGhCvBvBvA|DjBlC|@hTvEfBNvABvISxUo@|JSlA@tBLt@LdAXv@ZnB~@~@t@xAnAf@f@xEdEzAbBzAnBlWp_@bAnBlBpE|@tChBdFhBjGzCnJrCdHp@jAxAxBlGvH~@|Ab@bAp@xBnCdMd@pBl@fBt@bB\\j@v@lAxUlUtCjCdBz@nJdIdJbIrH`H~F|EjDxCzAnBNb@HdAJ|Fb@jDTpHq@@"}';
$foo = json_decode($foo_json);

I wrote this code, but json_decode returns null, and json_last_error() returns "JSON_ERROR_SYNTAX". So I know there are some syntax errors, but I tried the string at JSONLint, they say Valid JSON.

Probably the reason why because the string is complicated and includes symbols. I do not understand what is wrong.

PHP5.6.33

CentOS release 6.6

The answer is here. The problem was backslashes, and I do not like to remove it. json_decode returns JSON_ERROR_SYNTAX but online formatter says the JSON is OK Above article says just remove it, in this case, I need to replace it.

$foo_json = str_replace('\\', '\\\\', $foo_json); 

I added this code, then it works fine.

Yuji Hamada
  • 179
  • 1
  • 10

4 Answers4

1

Look at the rules for strings in JSON:

JSON strings

A \ character has a very limited set of characters it can be followed with.

Your input violates this several times:

e.g. \\X and \\V

Remember that \ is a special character in PHP string literals so if you want an escaped \ in JSON, you need to:

  1. Escape the \ with another \ for JSON
  2. Escape each of them with another \ for PHP

… giving you 4 \ characters.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Try this below code

$foo_json =preg_replace('/\\\\/', '', $foo_json);
$foo = json_decode($foo_json);
  print_r( $foo);exit;

It's working fine.

VRB
  • 186
  • 14
-2

json_(de|en)code needs to have UTF8 data. Check your input if it is UTF8 and if NOT, convert your input to UTF-8 and then use json_(de|en)code to get it ;)

Paladin
  • 1,637
  • 13
  • 28
-2

This should result in a valid response. (Handles line breaks and special escapes)

$json = str_replace("\n","\\n",$foo_json );
$json = str_replace("\r","",$json);
$json = preg_replace('/([{,]+)(\s*)([^"]+?)\s*:/','$1"$3":',$json);
$json = preg_replace('/(,)\s*}$/','}',$json);
var_dump($json);
Bert Maurau
  • 979
  • 5
  • 21
  • Why the downvote? No need for UTF8 encoding or anything.. It's just becase of the escaped values that you need to correctly escape for PHP. The above code parsed your json string correctly.. – Bert Maurau Oct 01 '18 at 07:47
  • I tested it with the code from the question. The output is still invalid JSON and does not parse. – Quentin Oct 01 '18 at 07:50