Richard Crowley’s blog

JSON in PHP

Anyone using JSON and PHP should be aware of bug #50867, just opened after bug #41505 didn't actually fix the problem.  PHP’s handling of empty string keys is blatantly incorrect.  Observe:

$ ruby -rjson -e 'puts JSON.parse("{\"\":\"foo\"}").to_json'
{"":"foo"}
$
$ python -c 'import json; print(json.dumps(json.loads("{\"\":\"foo\"}")))'
{"": "foo"}
$
$ php -r 'echo json_encode(json_decode("{\"\":\"foo\"}")), "\n";'
{"_empty_":"foo"}
$

"_empty_"?  That’s totally what I said.  Thanks, PHP!

Mind your empty strings, everyone.