summaryrefslogtreecommitdiffstats
path: root/json_to_csv.pl
blob: 23f40b5ae4973f849222417ccb944481de278a61 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl -w
##############################
#
# Quick & Dirty Flickr JSON flattening script
#
# Author: Solomon Peachy <solomon.peachy@philips.com>
#
#############################
use strict;
use JSON;
use utf8;

my $text;
my $doc;

my @flist;
my @database;
my %cols;

binmode STDOUT, ':utf8';

opendir (my $dh, ".") || die;
while (readdir($dh)) {
    if (/json/) {
	push(@flist, $_);
    }
}

foreach my $file (@flist) {
    $text = `cat $file`;
    $doc = decode_json($text);
    my %record;
    foreach my $item (keys(%$doc)) {
	my $val = $$doc{$item};
	if (ref($val) eq "HASH") {
	    foreach my $subitem (keys(%$val)) {
		my $subval =  $$val{$subitem};
		$cols{"$item-$subitem"} = 1;
		$record{"$item-$subitem"} = $subval;
#		print "$item-$subitem = $subval\n";
	    }
	} elsif (ref($val) eq "ARRAY") {
	    my $i = 1;
	    foreach (@$val) {
		my $aval =  $$val[$i-1];
		if (ref($aval) eq "HASH") {
		    foreach my $subitem (keys(%$aval)) {
			my $subval =  $$aval{$subitem};
			$cols{"$item-$i-$subitem"} = 1;
			$record{"$item-$i-$subitem"} = $subval;
#			print "$item-$i-$subitem = $subval\n";
		    }
		} else {
		    $cols{"$item-$i"} = 1;
		    $record{"$item-$i"} = $aval;
#		    print "$item-$i = $aval\n";
		}
		$i++;
	    }
	} else {
	    $val =~ tr/\n/ /;
	    $cols{$item} = 1;
	    $record{$item} = $val;
#	    print "$item = $val\n";
	}
    }
    push @database, \%record;
}

#exit(0);
### DONE, emit output.
foreach (sort(keys(%cols))) {
    print "\"$_\",";
}
print "\n";

foreach my $row (@database) {
    foreach (sort(keys(%cols))) {
	if (defined($$row{$_})) {
	    print "\"$$row{$_}\",";
	} else {
	    print ",";
	}
    }
    print "\n";
};