php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41504 json_decode() incorrectly decodes JSON objects with empty string keys
Submitted: 2007-05-26 07:21 UTC Modified: 2008-11-18 10:58 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: jmorrow at jessejmorrow dot com Assigned: iliaa (profile)
Status: Closed Package: JSON related
PHP Version: 5.2.2 OS: OS X
Private report: No CVE-ID: None
 [2007-05-26 07:21 UTC] jmorrow at jessejmorrow dot com
Description:
------------
The json_decode() function improperly and incosistently decodes JSON 
objects that have an empty string as one of their object keys/property 
names. In some cases the server crashes, some cases work properly, and 
other cases return "_empty_" as the array key.

The "_empty_" key and crashes are show stoppers for me.

Having an empty object key/property such as {"": "value"} is totally 
valid, and even common, JSON.

As a side note, I'll open a separate bug for this, json_encode(array
(""=>"value")) does not work either.

Reproduce code:
---------------
1) var_dump(json_decode('{"":"value"}', true));
2) var_dump(json_decode('{"":"value", "key":"value"}', true));
3) var_dump(json_decode('{"key":"value", "":"value"}', true));

Expected result:
----------------
1) array(1) { [""]=> string(5) "value" }
2) array(2) { [""]=> string(5) "value" ["key"]=> string(5) "value" }
3) array(2) { ["key"]=> string(5) "value" [""]=> string(5) "value" }

Actual result:
--------------
1) Page fails to return - some kind of script crash
2) array(2) { ["_empty_"]=> string(5) "value" ["key"]=> string(5) 
"value" } (the "_empty_" key coming up is the most common case when 
there the key is an empty string)
3) array(2) { ["key"]=> string(5) "value" [""]=> string(5) "value" } 
(works as expected)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-26 20:20 UTC] iliaa@php.net
I am unable to reproduce the crash with 
var_dump(json_decode('{"":"value"}', true));

it returns "_empty_" => "value" for me. Analysis through valgrind does 
not report any errors either.

The issue of "" being returned as "_empty_" is a bug and will be fixed.
 [2007-05-26 20:27 UTC] jmorrow at jessejmorrow dot com
Awesome Iliaa - thanks for the quick reply.

Any idea when to expect the fix?
 [2007-05-27 16:31 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 03:01:29 2024 UTC