Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: JSON module

by LTjake (Prior)
on Aug 14, 2007 at 02:27 UTC ( #632366=note: print w/ replies, xml ) Need Help??


in reply to JSON module

Using fetchrow_hashref you can do something like:

use strict; use warnings; 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; } print objToJson( { myData => \@output } );

It'd be killer on RAM with a lot of rows, though...

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


Comment on Re: JSON module
Select or Download Code
Re^2: JSON module
by hallikpapa (Scribe) on Aug 14, 2007 at 05:54 UTC
    This works great from a command line thanks, but I was getting a Premature end of script headers error when trying to call it from the same place I was calling the PHP script. Still trying to figure out what that is all about. Do you think pushing a dynamic XML pushing it back to the browser instead of printing a file would be better than JSON for big data formats? I have never done any XML formatting in perl. Maybe I should use the print option to send it back to the browser? The reporting table and everything comes back to the screen great, and if I switch the call back to the php script, everything works great. So what should I do instead of just a normal print to get it to pass it back as a response to the browser? Thank you monks.

      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)

        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"},......

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2015-07-05 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls