debugging beginner

How to pretty print JSON data in Perl?

Question

How to pretty print JSON data in Perl?

Pretty printing JSON in Perl is a common task especially when debugging or logging JSON data to make it easier for humans to read. Instead of a compact, minified JSON string, pretty printing formats the JSON with indentation and line breaks.

The go-to module in core Perl distributions for handling JSON is JSON (available since Perl 5.10 in many installations). It provides convenient methods to convert data structures to JSON strings and supports pretty printing with minimal setup.

Key Concepts

  • encode_json - encodes a Perl data structure to a compact JSON string.
  • to_json - encodes but can take options for pretty printing.
  • pretty - an option or method to produce indented JSON output.

Perl uses "sigils" like $ for scalars, @ for arrays, and % for hashes. When encoding data, you generally pass a reference (\@array or \%hash).

Example: Pretty Printing JSON in Perl

use strict;
use warnings;
use JSON;

# Sample Perl data structure (a hash ref)
my $data = {
    name    => "Alice",
    age     => 30,
    hobbies => [ "reading", "cycling", "coding" ],
    address => {
        city   => "Wonderland",
        zipcode => "12345",
    },
};

# Create a JSON object with pretty printing enabled
my $json = JSON->new->utf8->pretty(1);

# Encode data to pretty JSON string
my $pretty_json = $json->encode($data);

print "Pretty JSON output:\n";
print $pretty_json;

This script shows how to:

  • Create a JSON object with JSON->new
  • Enable UTF-8 output (recommended)
  • Invoke pretty(1) to enable indented, human-readable formatting
  • Encode a Perl data structure (a hash reference) with encode

Running this produces nicely formatted JSON output, something like:

{
   "address" : {
      "city" : "Wonderland",
      "zipcode" : "12345"
   },
   "age" : 30,
   "hobbies" : [
      "reading",
      "cycling",
      "coding"
   ],
   "name" : "Alice"
}

Additional Notes and Gotchas

  • Use utf8 if your data contains non-ASCII characters to avoid encoding issues.
  • pretty(1) is a shortcut for enabling indentation and line breaks. For more control, you can use $json->pretty->indent etc.
  • The JSON module defaults to compact output without pretty.
  • In scalar context, encode_json returns a compact string, so it’s not suitable if you want pretty output.
  • When decoding JSON, the reverse methods are decode or decode_json.
  • Legacy modules like JSON::XS or Cpanel::JSON::XS enhance speed but may require installing from CPAN.

Summary

To pretty print JSON in Perl, use the core JSON module, enable pretty printing with pretty(1), and encode your data structure. This approach is beginner-friendly, efficient, and makes debugging much easier.

Verified Code

Executed in a sandbox to capture real output. • v5.34.1 • 29ms

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Pretty JSON output:
{
   "age" : 30,
   "address" : {
      "city" : "Wonderland",
      "zipcode" : "12345"
   },
   "name" : "Alice",
   "hobbies" : [
      "reading",
      "cycling",
      "coding"
   ]
}
STDERR
(empty)

Was this helpful?

Related Questions