php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63675 json_decode/encode messing up the json data
Submitted: 2012-12-03 14:16 UTC Modified: 2012-12-04 04:14 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: kiran dot joseph at gmail dot com Assigned:
Status: Closed Package: json (PECL)
PHP Version: 5.4.9 OS: Windows 7 Professional SP1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: kiran dot joseph at gmail dot com
New email:
PHP Version: OS:

 

 [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}}

Patches

facebook (last revision 2013-04-11 17:33 UTC by toniochavez6 at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-04 00:07 UTC] mail+php at requinix dot net
Well yeah. You passed $assoc=true to json_decode() so it gave you back an array, 
not an object. Thus serializing that (non-associative) array will output a JSON 
array and not an object.

Change to:
$json = json_decode($str);
 [2012-12-04 03:09 UTC] kiran dot joseph at gmail dot com
Then In similar lines, the element "to" also should be converted to an array, right?
Why is it not? What is special about message_tags element?
 [2012-12-04 03:30 UTC] mail+php at requinix dot net
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.
 [2012-12-04 04:14 UTC] kiran dot joseph at gmail dot com
-Status: Open +Status: Closed
 [2012-12-04 04:14 UTC] kiran dot joseph at gmail dot com
Guess i was in haste when i reported this. 
A stackoverflow shot would have nailed it.

Thanks for the explanation. 
Closing the report.
 [2012-12-04 04:14 UTC] kiran dot joseph at gmail dot com
Guess i was in haste when i reported this. 
A stackoverflow shot would have nailed it.

Thanks for the explanation. 
Closing the report.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 05:01:27 2024 UTC