use YAML::XS qw/ Load /; use JSON::PP; my $file = "jsonyaml.txt"; open my $fh, "<", $file or die $!; my $yaml = do { local $/; <$fh> }; close $fh; $yaml =~ s{^ *//.*$}{}mg; my $data = Load($yaml); my $coder = JSON::PP->new->ascii->pretty; print $coder->encode($data);