Skip to content

Instantly share code, notes, and snippets.

@hiromi2424
Created April 21, 2011 10:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiromi2424/934136 to your computer and use it in GitHub Desktop.
Save hiromi2424/934136 to your computer and use it in GitHub Desktop.
simple json formatter(possible to use formatted)
// almost same output can be seen at:
// http://jsonformatter.curiousconcept.com/
class JsonDebug {
function format($json, $output = false) {
$depth = 0;
$length = strlen($json);
$result = '';
for ($n = 0; $n < $length; $n++) {
$c = $json{$n};
if ($c === '}' || $c === ']') {
$depth--;
$result .= self::__break($depth);
}
$result .= $c;
if ($c === '{' || $c === '[') {
$depth++;
$result .= self::__break($depth);
}
if ($c === ',') {
$result .= self::__break($depth);
}
}
if ($output) {
echo $result;
}
return $result;
}
function __break($depth) {
return "\n" . str_repeat("\t", $depth);
}
}
@jimmibond
Copy link

I have used this and created https://jsonformatter.org. thank you.

@m-rajesh
Copy link

Thanks. I could see same output in http://codeamaze.com/code-beautifier/json-beautifier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment