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

30 lines
496 B
Plaintext
Raw Permalink Normal View History

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