|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2008-11-18 03:13 UTC] Matt at mpcm dot com
 Description:
------------
json_decode() treats empty property name as "_empty_" not "". This was fixed in #41504 for arrays, but not for objects. (seems to happen in PHP Version 5.2.4-2ubuntu5.3 and 5.3.0alpha2.
Reproduce code:
---------------
<?
$s = '{"":"test"}';
var_dump(json_decode($s));
?>
Expected result:
----------------
object(stdClass)#1 (1) { [""]=>  string(4) "test" }
Actual result:
--------------
object(stdClass)#2 (1) { ["_empty_"]=>  string(4) "test" }
PatchesPull Requests
Pull requests: 
 HistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 01:00:01 2025 UTC | 
The language seems to create a key that cannot be reached, so even if this `bug` is fixed, we am still facing a broader issue it seems. <? $key = ""; $o = (object) array($key=>4,"example"=>8); var_dump($o); print 'blank key test:' . (isset($o->$key)?'true':'false'); print $o->{$key}; var_dump($o->$key); ?> output: object(stdClass)#1 (2) { [""]=> int(4) ["example"]=> int(8) } blank key test:false<br /> <b>Fatal error</b>: Cannot access empty property in <b>PHPDocument1</b> on line <b>8</b><br /> All throws Notice: line 4 - Illegal member variable nameI am using freebsd 7.2 and php 5.2.10 the first test case var_dump(json_decode('{"":"test"}')); silently stops processing of the script The test case var_dump(json_decode('{"test2":5,"":6}')); Fatal error: Cannot access empty property I am not sure the second is expected behavior but considering json is often from feeds it would be nice to fail more gracefully.Works for me here with 5.2-dev and 5.3-dev object(stdClass)#1 (2) { ["test2"]=> int(5) ["_empty_"]=> int(6) }To illustrate the current collision state, as this came up on the JSMentors list recently. <?php $a = '{"_empty_":"a","":"b","":"c"}'; echo "as object: " . json_encode(json_decode($a)); echo "\n"; echo "as array: " . json_encode(json_decode($a, true)); PHP 5.4.17 (cli) (built: Aug 25 2013 02:03:38) Copyright (c) 1997-2013 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend TechnologiesWith an output of: as object: {"_empty_":"c"} as array: {"_empty_":"b","":"c"}Last post was a bad example, this is the actual output: as object: {"_empty_":"c"} as array: {"_empty_":"a","":"c"}So I just ran into this problem while making a satis repository script for composer. I don't see a good solution. If I `$composerJson = json_decode($composerJson, true)` into associative arrays, manipulate it, then `json_encode($composerJson)` then my "require-dev": {} turns into "require-dev": [] which composer chokes on. So obviously I can't read the composer file as associative arrays! That isn't this bug, I just want to show upfront that using the associative array parameter on my json_decode cannot solve my problem. So I do: `$composerJson = json_decode($composerJson, false)` and turn it into objects like this thread talks about. Except the most common composer.json contains something like this: "autoload": { "psr-4": { "": "src/" } } it reads it in fine, but when I do `json_encode($composerJson)` again... I get: "autoload": { "psr-4": { "_empty_": "src/" } } which composer then chokes on because of course "_empty_" has a different meaning than "". So I'm stuck doing a json_encode then using a hack, `$composerJson = str_replace('"_empty_":', '"":', $composerJson)` before writing it to file again. The _empty_ key does not seem to me to be the right answer here. Should composer change the way composer files for virtually all projects be written? Is this a composer bug? Or should json_encode and json_decode objects reproduce a blank key? I hope the answer is the later, but I cannot say for sure.I fully agree empty string is valid unicode string. The RFC does not mention validity of empty object property which is invalid in most languages. There is nothing we can do for this because object property name cannot be "". Any restrictions to property name apply as well for objects. However, we can cast object to array, array to object. [yohgaki@dev ~]$ php -n -r '$a = [""=>1]; $o = (object)$a; var_dump($o); $a = (array)$o; var_dump($a);' object(stdClass)#1 (1) { [""]=> int(1) } array(1) { [""]=> int(1) } It may be better to remove automatic "" -> "_empty_" conversion at some point. e.g. next minor release.('_empty_' == 0 ) equal true,why?