json-to-multicsv
json-to-multicsv copied to clipboard
Split a JSON file with hierarchical data to multiple CSV files
=pod
=head1 NAME
json-to-multicsv.pl - Split a JSON file with hierarchical data to multiple CSV files
=head1 SYNOPSIS
B<json-to-multicsv.pl> [ B<--path pathspec:handler> ... ] [ B<--file input-file> ] [ B<--table name> ]
=head1 DESCRIPTION
Read in a JSON file, process it according as specified by the B<--path> arguments, and output one or multiple CSV files with the same data in tabular format.
=head1 EXAMPLE
Assuming the following input file:
{ "item 1": { "title": "The First Item", "genres": ["sci-fi", "adventure"], "rating": { "mean": 9.5, "votes": 190 } }, "item 2": { "title": "The Second Item", "genres": ["history", "economics"], "rating": { "mean": 7.4, "votes": 865 }, "sales": [ { "count": 76, "country": "us" }, { "count": 13, "country": "de" }, { "count": 4, "country": "fi" } ] } }
And the following command line flags:
--path /:table:item --path //rating:column --path //sales:table:sales --path /*/genres:table:genres
You'd get the following output files, which can be joined together using the B<*._key> fields.
B<item.csv>:
item._key,item.rating.mean,item.rating.votes,item.title "item 1",9.5,190,"The First Item" "item 2",7.4,865,"The Second Item"
B<item.genres.csv>:
genres,item._key,item.genres._key sci-fi,"item 1",1 adventure,"item 1",2 history,"item 2",1 economics,"item 2",2
B<item.sales.csv>:
item._key,item.sales._key,sales.count,sales.country "item 2",1,76,us "item 2",2,13,de "item 2",3,4,fi
=head1 OPTIONS
=over 8
=item B<--file> I
Read the JSON input from I
=item B<--path> I Values matching I If multiple tables are nested, the key columns of all outer tables
will be also emitted in the inner tables. =item B<--path> I Values matching I If the value is a scalar, that value will be output to a column named
after the field containing the value as the column name. Note: Scalar
values have an implicit B If the value is an object, each of the fields of the object will be used
to to output a column with the name being based on both the name of that
field, and the name of the field that contained the object. =item B<--path> I The values matching I =item B<--path> I Values matching I =item B<--table> I Specifies the I =back =head1 PATHS AND PATHSPECS The path to a specific JSON value is determined by the following
rules: Paths are matched against with pathspecs. In a pathspec any of the
elements of the path can instead be replaced with a C<>, which will
match any element in that position (but not multiple adjacent ones).
That is, the pathspec C</a//c> will match C<a/b/c> but not C<a/b/b/c>. =head1 AUTHOR Juho Snellman, [email protected] =head1 LICENSE Standard MIT license =cut:I