Skip to content

Instantly share code, notes, and snippets.

@pingyen pingyen/jsonBeautifier.php

Last active Sep 24, 2019
Embed
What would you like to do?
Beautify JSON files
<?php
$path = call_user_func(function() {
global $argv;
return isset($argv[1]) === true ?
$argv[1] :
'.';
});
function findJSONFiles($path) {
$paths = array();
foreach (scandir($path) as $item) {
if ($item[0] === '.') {
continue;
}
$path2 = "$path/$item";
if (is_dir($path2) === true) {
$paths = array_merge($paths, findJSONFiles($path2));
}
if (substr($item, -5) !== '.json') {
continue;
}
$paths[] = $path2;
}
return $paths;
}
$paths = findJSONFiles($path);
foreach ($paths as $path) {
echo "$path\n";
$data = json_decode(file_get_contents($path));
file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.