How to handle JSON arrays in Perl?
Question
How to handle JSON arrays in Perl?
Handling JSON arrays in Perl is a common task when dealing with data serialization or web APIs. Perl's core distribution does not include JSON support, but the JSON module, which is widely used and typically installed by default in many environments, provides an easy way to parse and generate JSON data, including arrays.
In JSON, arrays are represented as ordered lists of values enclosed in square brackets []. When decoding JSON arrays in Perl, you'll receive a reference to a Perl array (ARRAY ref). You can then dereference it or manipulate it just like any Perl array.
Key concepts to understand:
JSON::decode_jsonparses JSON strings into Perl data structures.- JSON arrays become Perl array references (scalar refs to arrays).
- You dereference array refs with
@{$array_ref}or access elements with$array_ref->[$index]. - Context matters: printing an array ref directly prints something like
ARRAY(0x123456), so dereference first.
Runnable example:
use strict;
use warnings;
use JSON;
# Example JSON string containing an array of numbers and strings
my $json_text = q{ ["apple", "banana", "cherry", 42, true] };
# Decode JSON string into Perl data structures
my $perl_array_ref = decode_json($json_text);
# Confirm we have an array reference
print "Type: ", ref($perl_array_ref), "\n"; # Prints "ARRAY"
# Loop through the array elements and print them
foreach my $elem (@{$perl_array_ref}) {
# JSON booleans decode as Perl 1/0 (true/false)
if (!defined $elem) {
print "undef\n";
} else {
print "$elem\n";
}
}
# Encode a Perl array back to a JSON string
my $new_json = encode_json($perl_array_ref);
print "JSON output: $new_json\n";
# Result:
# Type: ARRAY
# apple
# banana
# cherry
# 42
# 1
# JSON output: ["apple","banana","cherry",42,true]
Explanation
- We use
decode_jsonto convert the JSON text into a Perl array reference. - The variable
$perl_array_refis a reference pointing to an array of scalars (strings, numbers, booleans). - Dereferencing with
@{$perl_array_ref}allows us to loop through each element. - Booleans in JSON become 1 or 0 in Perl;
truebecomes 1. encode_jsonconverts the Perl data back into JSON format.
Common pitfalls
- Printing an array reference directly prints the reference address, not the contents. Always dereference when printing.
- JSON booleans become Perl integers (no native boolean type), so test accordingly.
- Malformed JSON will cause
decode_jsonto die—wrap in anevalor check witheval { decode_json($json) }to handle errors gracefully. - If you see
"ARRAY(0x...)", remember to dereference your array refs.
This simple workflow using the JSON module works for JSON arrays as well as nested arrays and hashes—Perl's flexible data structures and the module's encoding/decoding methods make JSON handling straightforward.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 20ms
Type: ARRAY
apple
banana
cherry
42
1
JSON output: ["apple","banana","cherry","42",true]
(empty)