Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: JSON module

by LTjake (Prior)
on Aug 14, 2007 at 12:14 UTC ( [id://632466]=note: print w/replies, xml ) Need Help??


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

The only difference would be that you need to send back the proper HTTP headers first before the JSON data:

use strict; use warnings; use CGI; use DBI; use JSON; my @output; my $dbh = DBI->connect('dbi:Pg:dbname=foo','bar','baz'); my $sth = $dbh->prepare('select * from qux'); $sth->execute; while ( my $row = $sth->fetchrow_hashref ){ push @output, $row; } my $cgi = CGI->new; print $cgi->header( 'application/json' ); print objToJson( { myData => \@output } );

--
"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)

Replies are listed 'Best First'.
Re^4: JSON module
by hallikpapa (Scribe) on Aug 14, 2007 at 20:30 UTC
    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"},......

      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"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://632466]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-03-19 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found