Created
February 25, 2013 01:29
-
-
Save sulrich/5026737 to your computer and use it in GitHub Desktop.
script to parse nested json in database dumps for kathy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/perl | |
use JSON; | |
use Data::Dumper; | |
my $debug = 0; | |
my @json_dump = (); | |
open(JSON, "$ARGV[0]") || die "error opening: $ARGV[0]\n"; | |
while(<JSON>) { | |
push(@json_dump, $_); | |
} | |
close(JSON); | |
my $header_flag = 0; | |
foreach my $call (@json_dump) { | |
print "new call\n" . '=' x 70 . "\n" if ($debug >= 1); | |
# rip off the pre/post scoreData JSON wrapper | |
$call =~ s/^\"\{ scoreData\:\[\{//; | |
$call =~ s/\]\}\"$//; | |
$call =~ s/\\r\\n/ /g; | |
print "\nraw call string\n" . '=' x 70 . "\n" if ($debug >= 3); | |
print "$call \n" if ($debug >= 3); | |
# split the caller info into fields | |
my @screens = split(/\}\,\{/, $call); | |
# initialize various call variables | |
my ($totalSupclicks, $nullSupclicks, $totalRepeats) = ""; | |
my %actionScore = ""; | |
foreach my $element (@screens) { | |
print "new screen\n" . '=' x 70 . "\n" if ($debug >= 1); | |
$element =~ s/^\"\{ scoreData\:\[\{//; | |
print "\nraw screen field\n" . '=' x 70 . "\n" if ($debug >= 3); | |
print $element . "\n\n" if ($debug >= 3); | |
$element =~ s/\"{2}/\"/g; # remove the duplicate double quotes | |
$element = '{ ' . $element . ' }'; | |
my $obj = jsonToObj($element); | |
print Dumper($obj) . "\n" . '-' x 70 . "\n" if ($debug >= 2); | |
$totalSupclicks = $totalSupclicks + $obj->{extra}->{supervisorClicks}; | |
if ( !defined($obj->{extra}->{supervisorClicks}) ) { | |
$nullSupclicks++; | |
} | |
# repeats ---------------------------------------------------- | |
# note - if repeats is null - this gets blessed into a | |
# JSON::NotString object, skip it. | |
if ( ref($obj->{repeats}) ne "JSON::NotString" ) { | |
$totalRepeats = $totalRepeats + $obj->{repeats}; | |
} | |
if ($obj->{actionCode} =~ /_NV\d+$/) { | |
$actionScore{$obj->{actionCode}} = $obj->{score}; | |
} | |
if ($obj->{actionCode} =~ /_AB\d+$/) { | |
$actionScore{$obj->{actionCode} . "_RESPONSE"} = $obj->{values}->[0]; | |
$actionScore{$obj->{actionCode} . "_SCORE"} = $obj->{values}->[1]; | |
# capture the dimension | |
$actionScore{$obj->{actionCode} . "_DIM"} = $obj->{dimension}; | |
} | |
if ($obj->{actionCode} =~ /_KS\d+$/) { | |
# capture the time | |
$actionScore{$obj->{actionCode} . "_TIME"} = $obj->{time}; | |
# capture the score | |
$actionScore{$obj->{actionCode} . "_NV"} = $obj->{score}; | |
# parse the keyboard entry values | |
my $val_ref = $obj->{values}; | |
for my $idx (0 .. (@$val_ref - 1) ) { | |
print "$idx: " . $obj->{values}->[$idx] . "\n" if ($debug >= 1); | |
my $key = $obj->{actionCode} . "_" . $idx; | |
$actionScore{$key} = $obj->{values}->[$idx]; | |
} | |
} | |
if ($obj->{actionCode} =~ /_NOTE$/) { | |
# parse the keyboard entry values | |
my $note_val_ref = $obj->{values}; | |
my $note_val = ""; | |
for my $note_idx (0 .. (@$note_val_ref - 1) ) { | |
print "$note_idx: " . $obj->{values}->[$note_idx] . "\n" if ($debug >= 1); | |
$note_val .= $obj->{values}->[$note_idx]; | |
} | |
# escpe the data nicely | |
$note_val =~ s/\"/\"\;/g; | |
$note_val =~ s/\'/\&rsquo\;/g; | |
$actionScore{$obj->{actionCode}} = '"' . $note_val . '"'; | |
} | |
if ($obj->{actionCode} =~ /_EMAIL$/) { | |
# parse the keyboard entry values | |
my $email_val_ref = $obj->{values}; | |
my $email_val = ""; | |
for my $email_idx (0 .. (@$email_val_ref - 1) ) { | |
print "$email_idx: " . $obj->{values}->[$email_idx] . "\n" if ($debug >= 1); | |
$email_val .= $obj->{values}->[$email_idx]; | |
} | |
# escpe the data nicely | |
$email_val =~ s/\"/\"\;/g; | |
$email_val =~ s/\'/\&rsquo\;/g; | |
$actionScore{ $obj->{actionCode} } = '"' . $email_val . '"'; | |
} | |
} # end of processing an activeElement on a caller | |
# check to see if this is the first time that we're outputting | |
# something. if so, put the field names on the first line | |
if ($header_flag == 0) { | |
print "totalSupclicks\t"; | |
print "nullSupclicks\t"; | |
print "totalRepeats\t"; | |
foreach my $i (sort keys(%actionScore)) { | |
next if $i eq ""; | |
print "$i\t"; | |
} | |
print "\n"; | |
$header_flag = 1; # set to prevent output of the headers again | |
} | |
# output for call ------------------------------------------------ | |
print "call output\n" if ($debug >= 1); | |
print '-' x 70 . "\n" if ($debug >= 1); | |
print "$totalSupclicks\t"; | |
print "$nullSupclicks\t"; | |
print "$totalRepeats\t"; | |
foreach my $i (sort keys(%actionScore)) { | |
next if $i eq ""; | |
print "$actionScore{$i}\t"; | |
} | |
print "\n"; | |
print '-' x 70 . "\n" if ($debug >= 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment