Created
May 7, 2012 18:13
-
-
Save preaction/2629401 to your computer and use it in GitHub Desktop.
Pretty-print a JSON file from STDIN or multiple filenames as arguments
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 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