How can I convert Mailgun JSON logs into CSV using Perl?

Seeking a concise Perl solution to convert Mailgun JSON logs into CSV output. E.g., process using regex substitutions:

my $jsonData = '{"location":{"town":"AlphaTown","state":"BetaState","nation":"USA"},"web_link":"https://site.example/reg/123","time_val":"9876543210"}';
$jsonData =~ s/.*"town":"([^"]+)".*"state":"([^"]+)".*"nation":"([^"]+)".*"web_link":"([^"]+)".*"time_val":"([^"]+)".*/"$1,$2,$3,$4,$5"/;
print $jsonData;

In practical experience I have come to rely on using Perl modules such as JSON::MaybeXS for parsing Mailgun JSON logs rather than crafting complex regex expressions. Although regex can seem appealing for its brevity, it is often brittle when the JSON structure changes or keys are reordered. I ran into issues with malformed input and unexpected key order which led me to sanitize the data and then use a module like Text::CSV for generating reliable CSV output. This approach ensures better error handling and maintainability in production environments.