... use JSON; use MIME::Base64; my $ret_data = { 'status' => 5, # success 'message' => "here is the file $filename you asked" 'message-html' => "success blah blah for $fileame", 'data' => MIME::Base64::encode_base64($image_binary_data) }; # if using CGI.pm use CGI; my $cgi = CGI->new; print $cgi->header(-type => "application/json", -charset => "utf-8"); print JSON::encode_json($ret_data); # or if using Mojolicious $c->render(json => $ret_data); #### my $ret_data = { 'status' => -1, # file was too big and I truncated it 'message' => "here is the file $filename you asked but it is truncated" 'message-html' => "success blah blah for $fileame but truncated", 'data' => truncate($data) };