Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

In my code I have this exmaple
my $RecType=uc($q->param('SearchRecType')); my $RecSubType=uc($q->param('SearchRecSubType')); my $SQL = "SELECT OBJ_ID,TITLE FROM OBJTBL"; $sth = $dbh->prepare($SQL); $sth->execute() or die "Can't execute Statement: $dbh->DBI::errstr"; my @rows; while ( my $row = $sth->fetchrow_hashref ) { push @rows, $row; } print $q->header( 'application/json' ); print encode_json( { results => \@rows } );
The JSON printed is like
{"results":[{"TITLE":"Title 1"OBJ_ID":"1"},{"TITLE":"Title 2","OBJ_ID" +:"2"}]}
I'm using typeahead.js whcih does not expect the reslts: part. I can't figure out how to return the JSON one level down, without it all being a child of "results".

Replies are listed 'Best First'.
Re: Returning JSON problem
by marto (Cardinal) on Oct 17, 2013 at 16:35 UTC

    Change:

    print encode_json( { results => \@rows } );

    To:

    print encode_json( \@rows );