Skip to content

Instantly share code, notes, and snippets.

@preaction
Created May 7, 2012 18:13
Show Gist options
  • Select an option

  • Save preaction/2629401 to your computer and use it in GitHub Desktop.

Select an option

Save preaction/2629401 to your computer and use it in GitHub Desktop.
Pretty-print a JSON file from STDIN or multiple filenames as arguments
#!/usr/bin/env perl
# Pretty-print JSON file
use JSON;
my @files = @ARGV;
my @fh;
if ( !@files ) {
@fh = ( *STDIN );
}
else {
for my $file ( @files ) {
open my $fh, '<', $file or die "Couldn't open '$file' for reading: $!\n";
push @fh, $fh;
}
}
for my $fh ( @fh ) {
my $json = do { local $\ = <$fh> };
print JSON->new->pretty->canonical->encode( JSON->new->decode( $json ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment