|  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
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
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:
		"level2": [1,2,3]

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


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]
Automatic comment from SVN on behalf of aharvey
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]
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: JSON related -Assigned To: +Assigned To: aharvey
 [2010-09-16 18:21 UTC]
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
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]
This is a PHP 5.4 feature.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 20:01:30 2024 UTC