1
0
.dotfiles/home/bin/json-pretty

30 lines
496 B
Perl
Executable File

#!/usr/bin/perl
# json-pretty: Take a JSON file and pretty-print it.
#
# Usage: json-pretty <file.json or url>
#
# --Kirsle
use strict;
use warnings;
use LWP::Simple;
use JSON;
my $file = shift(@ARGV) or die "Usage: $0 <file.json or url>\n";
my $json = JSON->new->utf8->pretty();
my $text = "";
if ($file =~ /^https?:/i) {
$text = get $file;
} else {
local $/ = undef;
open(my $fh, "<", $file);
$text = <$fh>;
close($fh);
}
my $data = $json->decode($text);
print $json->encode($data);