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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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: Sun Sep 28 01:00:01 2025 UTC