How to decode JSON string to Perl data structure?

Question

How to decode JSON string to Perl data structure?

Decoding a JSON string into a Perl data structure is a common task when working with web APIs, configuration files, or any data interchange format that uses JSON. In Perl, the most popular and straightforward way to decode JSON is by using the core module JSON (which comes with recent Perl distributions as part of JSON::PP or via the external JSON module on CPAN). This module allows converting JSON text into native Perl data structures such as hashes and arrays.

Basic Steps to Decode JSON in Perl

  • Import the decode_json function from the JSON module.
  • Pass the JSON string to decode_json. It returns a reference to the corresponding Perl data structure.
  • Dereference and use the data as normal Perl data.

Here is a simple runnable example showing this process:

use strict;
use warnings;
use JSON qw(decode_json);

# A sample JSON string representing a hash with nested array
my $json_text = q|{
  "name": "Alice",
  "age": 30,
  "languages": ["Perl", "Python", "JavaScript"]
}|;

# Decode JSON string to Perl data structure (hash ref)
my $perl_data = decode_json($json_text);

# Access and print elements from the Perl data structure
print "Name: " . $perl_data->{name} . "\n";
print "Age: " . $perl_data->{age} . "\n";
print "Languages:\n";

foreach my $lang (@{ $perl_data->{languages} }) {
    print "  - $lang\n";
}

Explanation of Perl-Specific Concepts

  • Sigils: In $perl_data->{name}, the $ indicates a scalar, and the arrow -> indicates dereferencing a hash reference to access its key name. For arrays (like languages), @{} dereferences the array reference.
  • Data Structure Types: JSON objects are decoded as Perl hash references, and JSON arrays as Perl array references.
  • Context: The decoded result is always a scalar reference (to hash or array), so you access elements accordingly.

Notes, Pitfalls, and Version Compatibility

  • JSON (or JSON::PP) is included in core Perl since 5.14, but you may want to install the CPAN JSON module for better performance and features.
  • If the JSON string is malformed, decode_json will die with an error. You can catch errors using eval to avoid crashing your program.
  • Unicode in JSON is handled automatically, but be sure your Perl script and terminal support UTF-8 if your JSON contains non-ASCII characters.
  • Be mindful about the difference between encode_json and decode_json: the former converts Perl to JSON string, and the latter does the opposite.

Summary

To summarize, decoding a JSON string to Perl data requires using decode_json from the JSON module, which converts JSON text into familiar Perl data structures. Understanding references and Perl's sigils is important to access the data. This approach is quite beginner-friendly and crucial for modern Perl programming involving JSON data interchange.

Verified Code

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

Tip: edit code and use “Run (Browser)”. Server runs always execute the published, verified snippet.
STDOUT
Name: Alice
Age: 30
Languages:
  - Perl
  - Python
  - JavaScript
STDERR
(empty)

Was this helpful?

Related Questions