Browse Source

Make json-pretty support URLs too

master
Noah Petherbridge 8 years ago
parent
commit
e9e5328a0d
  1. 18
      home/bin/json-pretty

18
home/bin/json-pretty

@ -2,22 +2,28 @@
# json-pretty: Take a JSON file and pretty-print it.
#
# Usage: json-pretty <file.json>
# 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>\n";
my $file = shift(@ARGV) or die "Usage: $0 <file.json or url>\n";
my $json = JSON->new->utf8->pretty();
local $/ = undef;
open(my $fh, "<", $file);
my $text = <$fh>;
close($fh);
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);

Loading…
Cancel
Save