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