use strict; use warnings; use JSON::XS; my $hash {}; ... fill up the hashref with stuff ... my $json = JSON::XS->new()->pretty(); my $json_text = $json->encode($hash); open(my $ofh, '>', $outfilename); print $ofh $json_text; close $ofh;