Last active
August 29, 2015 14:11
-
-
Save felipecwb/84062e6300e0a201edc8 to your computer and use it in GitHub Desktop.
A simple JSON pretty view written in PHP.
This file contains hidden or 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
#!/usr/bin/env php | |
<?php | |
define('TITLE', PHP_EOL . "Json Pretty print - @felipecwb" . PHP_EOL); | |
function showHelp() { | |
echo TITLE; | |
echo "\tUsage:" . PHP_EOL; | |
echo "\t- jsonlint [file.json]" . PHP_EOL; | |
echo "\t- string.json | jsonlint" . PHP_EOL . PHP_EOL; | |
exit(1); | |
} | |
array_shift($argv); | |
if (count($argv) != 0) { | |
if (is_file($argv[0])) { | |
$jsonString = file_get_contents($argv[0]); | |
} else { | |
showHelp(); | |
} | |
} elseif ($jsonString = stream_get_contents(STDIN)) { | |
if (empty($jsonString)) { | |
echo TITLE; | |
echo "*ERROR: empty data." . PHP_EOL; | |
exit(2); | |
} | |
} else { | |
showHelp(); | |
} | |
$data = json_decode($jsonString); | |
if (! $data) { | |
echo TITLE; | |
echo "*ERROR: Probably Sintax Error on JSON." . PHP_EOL; | |
exit(3); | |
} | |
echo PHP_EOL; | |
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_BIGINT_AS_STRING); | |
echo PHP_EOL; |
This file contains hidden or 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
echo '{"name":"Felipe Francisco"}' > test.json | |
./jsonview.php test.json | |
rm test.json | |
# really useful to webservice's return | |
curl http://date.jsontest.com/ | ./jsonview.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment