Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jsnell/json-to-multicsv

Split a JSON file with hierarchical data to multiple CSV files
https://github.com/jsnell/json-to-multicsv

csv data-conversion json

Last synced: about 2 months ago
JSON representation

Split a JSON file with hierarchical data to multiple CSV files

Awesome Lists containing this project

README

        

=pod

=head1 NAME

json-to-multicsv.pl - Split a JSON file with hierarchical data to multiple CSV files

=head1 SYNOPSIS

B [ 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._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:

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._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:B:I

Values matching I should be used to open a new table, with
the specified I. The value should be either an object or an
array. For an object, each field of the object will be used to output
a row in the CSV file corresponding to the new table. The name of the
field stored in the B._key column. For an array, each
element of the array will be used to output a row, with the index of
the element (starting from 1) stored in the B._key column.

If multiple tables are nested, the key columns of all outer tables
will be also emitted in the inner tables.

=item B<--path> I:B

Values matching I should be used to emit one or more columns
in the CSV file matching the innermost currently open table, on the
currently open row. (If no table is currently open).

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 handler.

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:B

The values matching I will be emitted as new rows. The value
must be an object. The name of the field containing the value will be
ignored. This is generally only useful for the toplevel JSON value.

=item B<--path> I:B

Values matching I (and any of their subvalues) will not
be processed at all.

=item B<--table> I

Specifies the I of the toplevel table, assuming the toplevel
JSON value is not used to define a table but row data. You will
probably want to use a B handler for the toplevel element.

=back

=head1 PATHS AND PATHSPECS

The path to a specific JSON value is determined by the following
rules:

- The path of the root element is /
- The path of a value that's directly contained inside an object
is the concatenation of: a) the path of the parent object, b)
the '/', c) the field in the object that this value is for.
- The path of a value that's directly contained inside an array
is the concatenation of: a) the path of the parent object, b)
the '/', c) the 1-based index in the array of the value.

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 will match C but not C.

=head1 AUTHOR

Juho Snellman,

=head1 LICENSE

Standard MIT license

=cut