php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44331 Formatting option for json_encode
Submitted: 2008-03-04 20:05 UTC Modified: 2011-07-21 11:35 UTC
Votes:10
Avg. Score:4.2 ± 0.7
Reproduced:9 of 9 (100.0%)
Same Version:4 (44.4%)
Same OS:3 (33.3%)
From: jeremy at duckwizard dot com Assigned: aharvey (profile)
Status: Closed Package: JSON related
PHP Version: 5.2.5 OS: Debian Stable
Private report: No CVE-ID: None
 [2008-03-04 20:05 UTC] jeremy at duckwizard dot com
Description:
------------
One of the appeals of JSON is that it is human readable.  However, json_encode produces output that is not particularly manageable by a human - little more so than PHP's own serialization.  DOM, for example, has a formatOutput option that nicely indents the resulting XML.  A similar option for json_encode would be extremely helpful.

Reproduce code:
---------------
$obj = new stdClass;
$obj->field1 = "hello";
$obj->field2 = "world";
$obj->field3 = array("key" => "value", "level2" => array(1,2,3));
echo json_encode($obj, JSON::FORMAT_OUTPUT);

//flags are an implementation example

Expected result:
----------------
{
	"field1":"hello",
	"field2":"world",
	"field3":
	{
		"key":"value",
		"level2": [1,2,3]
	}
}

Actual result:
--------------
{"field1":"hello","field2":"world","field3":{"key":"value","level2":[1,2,3]}}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-08 12:05 UTC] pankaj dot khairnar at live dot com
yes it will be a real nice feature of json_encode function
and I am expecting it to same.
 [2010-09-16 18:21 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&revision=303425
Log: Implement FR #44331 (Formatting option for json_encode). Bikeshedding about the
exact form of the JSON pretty printing and brace handling will only be accepted
in the form of patches. ;)
 [2010-09-16 18:21 UTC] aharvey@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: JSON related -Assigned To: +Assigned To: aharvey
 [2010-09-16 18:21 UTC] aharvey@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

The trunk version of json_encode() will now accept a JSON_PRETTY_PRINT option.
 [2011-04-29 19:11 UTC] php at mjpa dot co dot uk
This has not been implemented...
 [2011-07-21 09:04 UTC] joined_up_typeski at yahoo dot com
PHP Version 5.3.5-pl0-gentoo
JSON 1.2.1

passing JSON_PRETTY_PRINT to the function means that I get no output at all from PHP.
 [2011-07-21 11:35 UTC] rasmus@php.net
This is a PHP 5.4 feature.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 15:01:29 2024 UTC