#!/usr/bin/perl -w use strict; use warnings; use CGI; use DBI; use JSON; my %output=(); my $dbh = DBI->connect("dbi:mysql:database=test","usern","userp") or die "Connecting from Perl to MySQL database failed: $DBI::errstr"; my $sth = $dbh->prepare('SELECT name, email FROM user_tb'); $sth->execute; while ( my $row = $sth->fetchrow_hashref ){ push @{$output{'records'}}, $row; } my $cgi = CGI->new; print $cgi->header( 'application/json' ); print to_json( \%output );