php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50867 json_encode can't handle empty string keys
Submitted: 2010-01-27 22:32 UTC Modified: 2010-01-28 08:25 UTC
From: r at rcrowley dot org Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.3.1 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: r at rcrowley dot org
New email:
PHP Version: OS:

 

 [2010-01-27 22:32 UTC] r at rcrowley dot org
Description:
------------
The specification outlined at http://json.org/ indicates that the empty 
string is a perfectly valid key in a JSON data structure.  JSON 
implementations in Python and Ruby agree.  PHP appears to go out of its 
way to handle the empty string as as key in a non-standard way.

Reproduce code:
---------------
$ php -r 'echo json_encode(json_decode("{\"\":\"foo\"}")), "\n";'
{"_empty_":"foo"}
$

Expected result:
----------------
I expect to see exactly the same output from PHP as seen from Ruby and 
Python:

$ ruby -rjson -e 'puts JSON.parse("{\"\":\"foo\"}").to_json'
{"":"foo"}
$ python -c 'import json; 
print(json.dumps(json.loads("{\"\":\"foo\"}")))'
{"": "foo"}
$

Actual result:
--------------
$ php -r 'echo json_encode(json_decode("{\"\":\"foo\"}")), "\n";'
{"_empty_":"foo"}
$

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-28 08:25 UTC] jani@php.net
It can not work in PHP since you can not have empty object property name. 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC