summaryrefslogtreecommitdiff
path: root/share/json2csv
blob: 49aead912083cdf4d884d19143438f8015fda188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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', 'EVA (not always identical with UIC ID)', 'Latitude', 'Longitude' );
$buf .= $csv->string;

for my $station ( @{$stations} ) {
	my @fields = ( $station->{name}, $station->{ds100}, $station->{eva} );
	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 );