This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json2csv.pl
49 lines (35 loc) · 1.95 KB
/
json2csv.pl
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
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/perl
##use local::lib;
use warnings;
use strict;
##perl Makefile.PL PREFIX=/Users/melt/Dropbox/ORU/GMS/SarsCov2-pipeline/sarscov2/PerlLibs/
##make
##make test
##make install
##export PERL5LIB=/Users/melt/Dropbox/ORU/GMS/SarsCov2-pipeline/sarscov2/PerlLibs/lib/perl5/site_perl/
use JSON;
use Text::CSV_XS;
use File::Slurp;
my $str = read_file('covidMetadataTemplate.json');
my $json = '[ {
"name" : "Lee calls",
"empid" : "1289328",
"desc_id" : "descl23423..23431"
},
{
"name" : "Lee calls",
"empid" : "23431223",
"desc_id" : "descl23423..234324"
} ]';
##my $struct = decode_json($json);
my $struct = decode_json($str);
##my %hash = decode_json($json);
##print $struct . "\n" ;
my $csv = 'Text::CSV_XS'->new({ binary => 1, eol => "\n",sep_char=> ";" });
open my $OUT, '>:encoding(UTF-8)', 'output.csv' or die $!;
print $OUT "Internal_lab_ID;Sminet_LID;Pseudo_ID;Region_code;Lab_code;Selection_criterion;GISAID_acc;Submitter;Type;Passage_details;Collection_date;Host;Patient_sex;Patient_age;Patient_status;Sequencing_technology;Library_method;Lane;Fastq1;Fastq2;Fast5;Seq_path;Comment\n";
##$csv->print($OUT, [ @$_{qw{name empid desc_id }} ]) for @$struct;
##$csv->print($OUT, [ @$_{qw{Internal_lab_ID Sminet_LID Pseudo_ID }} ]) for @$struct;
$csv->print($OUT, [ @$_{qw{ Internal_lab_ID Sminet_LID Pseudo_ID Region_code Lab_code Selection_criterion GISAID_acc Submitter Type Passage_details Collection_date Host Patient_sex Patient_age Patient_status Sequencing_technology Library_method Lane Fastq1 Fastq2 Fast5 Seq_path Comment }} ]) for @$struct;
close $OUT or die $!;
##Internal_lab_ID;Sminet_LID;Pseudo_ID;Region_code;Lab_code;Selection_criterion;GISAID_acc;Submitter;Type;Passage_details;Collection_date;Host;Patient_sex;Patient_age;Patient_status;Sequencing_technology;Library_method;Lane;Fastq1;Fastq2;Fast5;Seq_path;Comment