Last active
September 24, 2019 16:06
-
-
Save pingyen/2415f3a62e96f3685e56744c737130a9 to your computer and use it in GitHub Desktop.
Beautify JSON files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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