Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Formatting JSON the right way

by derby (Abbot)
on Aug 20, 2014 at 12:26 UTC ( [id://1098106]=note: print w/replies, xml ) Need Help??


in reply to Formatting JSON the right way

Looks like you're just lower casing your result set keys ... you should be able to simplify via DBI's FetchHashKeyName param. Added with a combination of selectall_arrayref. (code not tested):

!#/usr/bin/perl use strict; use warnings; use CGI::Carp qw(fatalsToBrowser); use CGI qw(-oldstyle_urls :standard); use DBI; use DBD::ODBC; use JSON; my $q = CGI->new; my $search = $q->param('search') || ""; my $user = "user"; my $pwd = "pwd"; my $dbh = DBI->connect("DBI:ODBC" ,$user ,$pass , { PrintError => 0, FetchHashKeyName => 'NAME_lc' } ) or die "Can not connect to ODBC database: $DBI::errstr\n" ; my $sql = qq{SELECT name,city,state from mytable where name like ? limit 5}; my $data = $dbh->selectall_arrayref( $sql, { Slice => {} }, $search.'%' ); print $q->header(-type => "application/json", -charset => "utf-8"); print encode_json( $data )
-derby

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-19 08:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found