php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52502 json_decode doesn't always work
Submitted: 2010-07-31 04:50 UTC Modified: 2010-07-31 05:34 UTC
From: zelnaga at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.3 OS: Windows 7
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: zelnaga at gmail dot com
New email:
PHP Version: OS:

 

 [2010-07-31 04:50 UTC] zelnaga at gmail dot com
Description:
------------
In particular, "{\"test\": [\"\n\"]}" doesn't give the correct result.  Examples follow.

Test script:
---------------
<?php
$a = "{\"test\": [\"\n\"]}";

echo "$a\r\n";

var_dump(json_decode($a, true));
?>

Expected result:
----------------
I would expect an array to be output.  ie.

array(1) {
  ["test"]=>
  array(1) {
    [0]=>
    string(1) "
"
  }
}

Certainly javascript has no problem with it:

<script src="http://github.com/kvz/phpjs/raw/master/functions/strings/echo.js"></script>
<script src="http://github.com/kvz/phpjs/raw/master/functions/var/var_dump.js"></script>
<script>
a = {"test": ["\n"]};

var_dump(a);
</script>

Actual result:
--------------
NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-31 05:34 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-07-31 05:34 UTC] rasmus@php.net
PHP uses the reference implementation from json.org.  json.org states that a new-
line in json is written as \n and not a literal newline.  If you can convince 
Douglas Crockford at json.org that this should be valid json, we will fix it.  
Otherwise, no chance.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Sep 27 23:00:01 2025 UTC