php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65996 json_decode have problem with \\
Submitted: 2013-10-29 15:14 UTC Modified: 2013-10-29 18:54 UTC
From: nadavvin at gmail dot com Assigned:
Status: Not a bug Package: json (PECL)
PHP Version: 5.5.5 OS:
Private report: No CVE-ID: None
 [2013-10-29 15:14 UTC] nadavvin at gmail dot com
Description:
------------
json_encode failed to parse if there is '\' or '\\' but if there more it represent all as one '\' even if there is five.

* Firefox can parse all the strings

Test script:
---------------
$a = '{"A":"C\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";

$a = '{"A":"C\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";

$a = '{"A":"C\\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";

$a = '{"A":"C\\\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";

$a = '{"A":"C\\\\\D"}';
var_dump(json_decode($a));
echo json_last_error()."\n";

Expected result:
----------------
all parse, and the result of '\\\\' should be '\\'

Actual result:
--------------
syntax error in '\', '\\'

'\\\', '\\\\', '\\\\\' all represent as one '\'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-10-29 16:00 UTC] anon at anon dot anon
No, it's correct. You have a JavaScript String inside a PHP string, so PHP itself is parsing slashes.
E.g. the PHP code '{"A":"C\\\\D"}' results in the string {"A":"C\\D"}. The embedded JavaScript string is C\D.
var_dump the $a strings before decoding, or read the JSON data from a file instead of embedding it in code, and you'll see what I mean.
 [2013-10-29 18:54 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2013-10-29 18:54 UTC] nikic@php.net
Not a bug, as explained by @anon's comment.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC