|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-12-03 14:16 UTC] kiran dot joseph at gmail dot com
Description:
------------
When decoded using json_decode and encoded back using json_encode some of the
json elements are getting erroneously encoded.
For eg: In the script below, the key named "message_tags" has the value
{ "0": [ { "id": "663869041", "name": "Kiran Kumar Joseph", "type": "user", "offset": 0, "length": 18 } ] }
When decoded and encoded back into json, the message_tags' value changes to this
[[{"id":"663869041","name":"Kiran Kumar Joseph","type":"user","offset":0,"length":18}]]
The key "0" has been lost and an extra array has been used to wrap the original
array.
-------------------
I am using this package. php-5.4.9-nts-Win32-VC9-x86.zip.
Though the reproduction is on Windows, I have seen similar behavior on Linux
also on older version of php. (PHP 5.2.10 (cli) (built: Jan 6 2011 10:28:31) on
Linux SMP x86_64 x86_64 x86_64 GNU/Linux
Test script:
---------------
<?php
$str = '{ "id": "663869041_10151341204554042", "from": { "name": "Kiran Kumar Joseph", "id": "663869041" }, "to": { "data": [ { "name": "Kiran Kumar Joseph", "id": "663869041" } ] }, "message": "Kiran Kumar Joseph TEsting", "message_tags": { "0": [ { "id": "663869041", "name": "Kiran Kumar Joseph", "type": "user", "offset": 0, "length": 18 } ] }, "actions": [ { "name": "Comment", "link": "https://www.facebook.com/663869041/posts/10151341204554042" }, { "name": "Like", "link": "https://www.facebook.com/663869041/posts/10151341204554042" } ], "privacy": { "description": "Glisa Tk", "value": "CUSTOM", "friends": "SOME_FRIENDS", "networks": "", "allow": "100001436109139", "deny": "" }, "type": "status", "status_type": "mobile_status_update", "created_time": "2012-11-21T15:01:26+0000", "updated_time": "2012-11-21T15:01:26+0000", "comments": { "count": 0 } }';
$json = json_decode($str, true);
echo json_encode($json);
?>
Expected result:
----------------
I expect to see this output.
{ "id": "663869041_10151341204554042", "from": { "name": "Kiran Kumar Joseph",
"id": "663869041" }, "to": { "data": [ { "name": "Kiran Kumar Joseph", "id":
"663869041" } ] }, "message": "Kiran Kumar Joseph TEsting", "message_tags": { "0":
[ { "id": "663869041", "name": "Kiran Kumar Joseph", "type": "user", "offset": 0,
"length": 18 } ] }, "actions": [ { "name": "Comment", "link":
"https://www.facebook.com/663869041/posts/10151341204554042" }, { "name": "Like",
"link": "https://www.facebook.com/663869041/posts/10151341204554042" } ],
"privacy": { "description": "Glisa Tk", "value": "CUSTOM", "friends":
"SOME_FRIENDS", "networks": "", "allow": "100001436109139", "deny": "" }, "type":
"status", "status_type": "mobile_status_update", "created_time": "2012-11-
21T15:01:26+0000", "updated_time": "2012-11-21T15:01:26+0000", "comments": {
"count": 0 } }
Actual result:
--------------
I see this instead.
{"id":"663869041_10151341204554042","from":{"name":"Kiran Kumar
Joseph","id":"663869041"},"to":{"data":[{"name":"Kiran Kumar
Joseph","id":"663869041"}]},"message":"Kiran Kumar Joseph TEsting","message_tags":
[[{"id":"663869041","name":"Kiran Kumar
Joseph","type":"user","offset":0,"length":18}]],"actions":
[{"name":"Comment","link":"https:\/\/www.facebook.com\/663869041\/posts\/101513412
04554042"},
{"name":"Like","link":"https:\/\/www.facebook.com\/663869041\/posts\/1015134120455
4042"}],"privacy":{"description":"Glisa
Tk","value":"CUSTOM","friends":"SOME_FRIENDS","networks":"","allow":"1000014361091
39","deny":""},"type":"status","status_type":"mobile_status_update","created_time"
:"2012-11-21T15:01:26+0000","updated_time":"2012-11-21T15:01:26+0000","comments":
{"count":0}}
Patchesfacebook (last revision 2013-04-11 17:33 UTC by toniochavez6 at gmail dot com)Pull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 21:00:02 2025 UTC |
What's special about message_tags is that it is a "non-associative" array, meaning that it contains only integer keys in sequential order. If the array contained any string keys* or if there were any "holes" in the number sequence (like 0, 1, 3) then it would be re-serialized as an object, but it does not so it gets the special treatment of becoming an actual JSON array. The "to" array contains a string key ("data") thus it becomes an object again. * PHP does not allow for string array keys which are whole numbers. "0" and 0 are interchangeable and the latter is the real one. Even though the original JSON has "0" as a string key, it becomes a regular integer key in the decoded PHP form.