Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Pretty print input JSON-formatted single lines into pretty huge array of JSON objects.
- use strict;
- use warnings;
- use JSON;
- our $json_output_config = { pretty => 1, utf8 => 0, ascii => 1, canonical => 1 };
- #use IO::Handle qw();
- die "Usage: script input.lines.json output.pretty.json" unless scalar @ARGV == 2;
- open my $f, "<$ARGV[0]" or die "Can't open input for read.";
- binmode $f;
- my @all;
- my $line = 0;
- while (<$f>) {
- next if /^\s*$/;
- printf STDERR ".";
- push @all, from_json($_);
- ++$line;
- }
- close $f;
- open my $g, ">:encoding(utf-8)","$ARGV[1]" or die "Can't open output for write.";
- print $g chr(0xFEFF) if $json_output_config->{utf8}; # BOM
- print $g to_json(\@all, $json_output_config);
- close $g;
Advertisement
Add Comment
Please, Sign In to add comment