Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Formatting JSON the right way

by derby (Abbot)
on Aug 20, 2014 at 12:26 UTC ( #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 )

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2022-12-07 07:58 GMT
Find Nodes?
    Voting Booth?

    No recent polls found