Select Git revision
json2tsv.pl
json2tsv.pl 3.16 KiB
#!/usr/bin/perl
=head1 USAGE
cat data.json-lines | ./json2tsv.pl
Reads individual lines which *each* contain a separate json structure
and saves the data in TSV format.
This is useful to save data from MongoDB find() statement using
cut'n'paste.
=cut
use strict;
use Data::Dumper;
$Data::Dumper::Indent= 1;
use JSON;
use Util::tsv;
my $tsv_fnm= 'data.tsv';
my @column_names;
my @PARS;
my $arg;
while (defined ($arg= shift (@ARGV)))
{
if ($arg eq '--') { push (@PARS, @ARGV); @ARGV= (); }
elsif ($arg =~ /^--(.+)/)
{
my ($opt, $val)= split ('=', $1, 2);
if ($opt eq 'help') { usage(); }
elsif ($opt eq 'out') { $tsv_fnm= $val || shift (@ARGV); }
elsif ($opt eq 'col')
{
$val= shift (@ARGV) unless ($val);
push (@column_names, split (',', $val));
}
else { usage(); }
}
elsif ($arg =~ /^-(.+)/)
{
foreach my $opt (split ('', $1))
{
if ($opt eq 'h') { usage(); exit (0); }
# elsif ($opt eq 'x') { $x_flag= 1; }
else { usage(); }
}
}
else
{
push (@PARS, $arg);
}
}
my @rows;
my %cols;
if (@PARS)
{
foreach my $fnm (@PARS)
{
if (open (FI, '<:utf8', $fnm))
{
parse_stream(*FI, \@rows, \%cols);
close (FI);
} # TODO: else complain
}