use JSON; sub remove_newlines { my $str = shift; $str =~ s/^\s+//gsm; $str =~ s/\n//gsm; return $str; } %HoA = ('foo' => [1,2,3], 'bar' => ['a','b','c']); print "plain:\n".JSON->new->encode(\%HoA)."\n"; my $pretty = JSON->new->pretty->encode(\%HoA); $pretty =~ s/(\[.*?\])/remove_newlines($1)/egsm; print "pretty:\n$pretty";