summaryrefslogtreecommitdiff
path: root/share/csv2json
blob: a91bac993061b00275829f14151fd49e9fe3dbdf (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
31
32
33
34
35
36
37
#!/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 @csv_lines = read_file( 'stations.csv', { binmode => ':utf8' } );
my @stations;
my $csv = Text::CSV->new;

# skip header
shift @csv_lines;

for my $line (@csv_lines) {
	if ( $csv->parse($line) ) {
		my ( $name, $ds100, $uic, $lat, $lon ) = $csv->fields;
		my $station = {
			name    => $name,
			ds100   => $ds100,
			uic     => 0 + $uic,
			latlong => undef
		};
		if ( $lat and $lon ) {
			$station->{latlong} = [ 0 + $lat, 0 + $lon ];
		}
		push( @stations, $station );
	}
}

@stations = sort { $a->{name} cmp $b->{name} } @stations;

my $json_out = JSON->new->utf8->canonical->pretty->encode( [@stations] );
write_file( 'stations.json', $json_out );