in reply to Re^3: JSON module
in thread JSON module

Of course! Thank you. So the data comes out formatted correctly that way, but then I try and do some manipulation with it and push it back into a hash. But it comes out improperly formatted and out of order. This is what I do
%hashData = ( id => $total_cdr_count, col2 => $total_call_count, col3 => $total_error_count, col4 => $formatMin, col5 => $formatASR, col6 => $formatPDD, col7 => $formatDUR, col8 => $hold_max_date ); push @output, %hashData;
And after encoding, it looks like this
{"myData":["col7",null,"col5",null,"col3",0,"col8","2007-08-10 00:59:5 +8","col2",0,"id",3951,"col4",null ,"col6",null,"col7",null,"col5",null,"col3",0,"col8","2007-08-10 00:59 +:58","col2",0,"id",3961,"col4" ,null,"col6",null,"col7",null,"col5",null,"col3",0,"col8","2007-08-10 +00:59:58","col2",0,"id"
the null is fine, I need to fix a few references, but it's out of order, and it looks like it is putting commas in places where a colon should be for json encoding? Also looks like it isn't putting the {} in between records. Here's an example of what it should look like coming out
{"myData":[{"id":"08-10-2007.00","col2":null,"col3":"I","col4":"0","co +l5":"3951","col6":null,"col7":"8028" ,"col8":"0"},......

Replies are listed 'Best First'.
Re^5: JSON module
by LTjake (Prior) on Aug 15, 2007 at 12:06 UTC

    Your hash is being flattened to a list in this line:

    push @output, %hashData;

    You really want a hash reference there:

    push @output, \%hashData;

    --
    "Go up to the next female stranger you see and tell her that her "body is a wonderland."
    My hypothesis is that she’ll be too busy laughing at you to even bother slapping you.
    " (src)

      <edit> Looks like I need an array of hashes, because the records were getting overwritten. But doing something like this will screw up the JSON:
      $counter++; $hashData{$counter}{'id'} = $hold_col_val1; $hashData{$counter}{'col2'} = $hold_col_val2; $hashData{$counter}{'col3'} = $hold_col_val3; $hashData{$counter}{'col4'} = $total_cdr_count; $hashData{$counter}{'col5'} = $total_call_count; $hashData{$counter}{'col6'} = $total_error_count; push @output, \%hashData;
      I appreciate your help with all of this. I haven't touched perl in a while and trying to get the hash to work. I comes out like this now, and the extra digit at the beginning of every record. Can it be removed before encoding it in JSON?
      $sth->execute(); while (my $row = $sth->fetchrow_hashref ){ push @newRow, $row; $gt_cdrs = $gt_cdrs + $row->{"col4"}; $gt_hold_time=$gt_hold_time + $row->{"col5"}; if ($row->{"CALL_STATUS"} eq "S" || $row->{"CALL_STATUS"} eq " +R" || $row->{"CALL_STATUS"} eq "I") { $gt_duration = $gt_duration + $row->{"col6"}; } else { $gt_errs = $gt_errs + $row->{"col4"}; } $gt_calls = ($gt_cdrs - $gt_errs); ################################# if (($hold_col_val1 ne $row->{"id"}) || ($hold_col_val2 ne $row->{"co +l2"}) || ($hold_col_val3 ne $row->{"col3"})) { unless ($hold_col_val1 eq "~~~") { ## calculate percentages $ASR = (($total_cdr_count-$total_error_count) +/ $total_cdr_count) * 100; if (($total_cdr_count-$total_error_count) > 0) + { $avgPDD = $good_hold_time / ($total_cd +r_count - $total_error_count); $avgDUR = $good_duration / ($total_cdr +_count - $total_error_count); } else { $avgPDD = 0; $avgDUR = 0; } $minutes = $good_duration / 60; if($asr_scrn ne "") { if( ($asr1 ne "" ) && ($asr2 eq "") ) +{ if ($ASR > $asr1) { goto EXTCHK1; } } if( ($asr1 ne "" ) && ($asr2 ne "") ) +{ if ( ($ASR < $asr1 ) || ($ASR +> $asr2+1) ) { goto EXTCHK1; } } } $formatASR = sprintf("%3.0d",$ASR); $formatPDD = sprintf("%3.0d",$avgPDD); $formatDUR = sprintf("%3.0d",$avgDUR); $formatMin = sprintf("%4.1f",$minutes); } $counter++; $hashData{'counter'} = $counter; $hashData{$counter}{'id'} = $hold_col_val1; $hashData{$counter}{'col2'} = $hold_col_val2; $hashData{$counter}{'col3'} = $hold_col_val3; $hashData{$counter}{'col4'} = $total_cdr_count; $hashData{$counter}{'col5'} = $total_call_count; $hashData{$counter}{'col6'} = $total_error_count; push @output, \%hashData; #print $hashData[$counter]{'id'}." ".$hashData[$counte +r]{'col6'}."\n"; }

        @output should be an array of hashrefs.

        while ( my $row = $sth->fetchrow_hashref ) { my %data; $data{ col1 } = 'bar'; push @output, \%data; }

        Then converting it to JSON will make it look something like (i've added some formatting):

        { "myData" : [ { "col1" : "bar" }, { "col1" : "bar" }, # etc ... ] }

        HTH.

        --
        "Go up to the next female stranger you see and tell her that her "body is a wonderland."
        My hypothesis is that she’ll be too busy laughing at you to even bother slapping you.
        " (src)