summaryrefslogtreecommitdiff
path: root/share/json2csv
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2019-11-17 20:58:53 +0100
committerDaniel Friesel <derf@finalrewind.org>2019-11-17 20:58:53 +0100
commit3d845879adfa68be120b57a2a57184ccd1cc337b (patch)
tree0e6e26f0b3ff99acb07b28c9d1e99c90a9424ffa /share/json2csv
parent76c660b3742c5b4605c5fc072afd0aeb0ec5b4cd (diff)
add csv2json and json2csv helpers for easier station list management
Diffstat (limited to 'share/json2csv')
-rwxr-xr-xshare/json2csv30
1 files changed, 30 insertions, 0 deletions
diff --git a/share/json2csv b/share/json2csv
new file mode 100755
index 0000000..0601f96
--- /dev/null
+++ b/share/json2csv
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use 5.010;
+
+use File::Slurp qw(read_file write_file);
+use JSON;
+use Text::CSV;
+
+my $json_str = read_file('stations.json');
+my $stations = JSON->new->utf8->decode($json_str);
+my $csv = Text::CSV->new( { eol => "\n" } );
+
+my $buf = '';
+
+$csv->combine( 'name', 'DS100', 'UIC (IBNR)', 'Latitude', 'Longitude' );
+$buf .= $csv->string;
+
+for my $station ( @{$stations} ) {
+ my @fields = ( $station->{name}, $station->{ds100}, $station->{uic} );
+ if ( $station->{latlong} ) {
+ push( @fields, $station->{latlong}[0], $station->{latlong}[1] );
+ }
+ if ( $csv->combine(@fields) ) {
+ $buf .= $csv->string;
+ }
+}
+
+write_file( 'stations.csv', { binmode => ':utf8' }, $buf );