in reply to converting JSON to CSV
Code..."Timestamp","Average","Unit" "2017-03-01T04:05:00Z","145.8","Count" "2017-03-01T10:35:00Z","182.2","Count" "2017-03-01T05:30:00Z","180.2","Count"
use warnings; use strict; use Text::CSV; use JSON::MaybeXS; my $json = <<'END_JSON'; { "Datapoints": [ { "Timestamp": "2017-03-01T04:05:00Z", "Average": 145.80000000000001, "Unit": "Count" }, { "Timestamp": "2017-03-01T10:35:00Z", "Average": 182.19999999999999, "Unit": "Count" }, { "Timestamp": "2017-03-01T05:30:00Z", "Average": 180.19999999999999, "Unit": "Count" } ], "Label": "DatabaseConnections" } END_JSON my $data = decode_json($json); my $csv = Text::CSV->new({auto_diag=>2, binary=>1, eol=>"\n", always_quote=>1 }); my @fields = qw/ Timestamp Average Unit /; $csv->print(select, \@fields); for my $datapoint ( @{ $data->{Datapoints} } ) { $csv->print(select, [ map {$datapoint->{$_}} @fields ]); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: converting JSON to CSV
by cbtshare (Monk) on Apr 07, 2017 at 01:50 UTC | |
by AnomalousMonk (Archbishop) on Apr 07, 2017 at 03:25 UTC | |
by shmem (Chancellor) on Apr 07, 2017 at 21:11 UTC | |
by haukex (Archbishop) on Apr 08, 2017 at 09:14 UTC | |
|
Re^2: converting JSON to CSV
by cbtshare (Monk) on Apr 07, 2017 at 00:10 UTC |