my %hash; foreach my $result (@$result) { my ($id, $location, $name, $gender) = @$result; %hash = ( "id" => $id, "location" => $location, "name" => $name, "gender" => $gender); push( @$result, \%hash ); } my $json = encode_json \%hash; print $CGI->header( -type => 'application/json' ),$json;