π« JSON with Perl β Encode, Decode, and Exchange Data (2025 Guide)
π§² Introduction β Why Use JSON in Perl?
Perl, known for its powerful text processing capabilities, supports JSON seamlessly through core modules like JSON, JSON::XS, and Cpanel::JSON::XS. These modules allow developers to encode Perl data structures into JSON and decode JSON into Perl hashes and arrays, making it ideal for building REST APIs, web services, and automation scripts.
π― In this guide, you’ll learn:
- How to encode and decode JSON in Perl
- Core modules and syntax
- Real-world examples for APIs and file handling
- Best practices for clean and efficient code
π¦ Step 1: Load JSON Module in Perl
Install via CPAN if not already available:
cpan JSON
Or include it in your script:
use JSON;
β Encoding Perl Data to JSON
π§ͺ Example β Convert Hash to JSON String
use JSON;
my %user = (
name => "Alice",
age => 30,
email => "alice@example.com"
);
my $json_text = encode_json(\%user);
print "$json_text\n";
π Output:
{"name":"Alice","age":30,"email":"alice@example.com"}
βοΈ encode_json() takes a reference to a hash or array and returns a JSON string.
π Decoding JSON to Perl Data
π§ͺ Example β JSON String to Hash
use JSON;
my $json_string = '{"name":"Bob","age":25}';
my $perl_hash = decode_json($json_string);
print "Name: $perl_hash->{name}\n"; # Output: Bob
βοΈ decode_json() turns JSON into a Perl reference (hashref or arrayref).
π‘ Reading JSON from a File
π§ͺ Example β Load and Decode JSON File
use JSON;
use strict;
use warnings;
open my $fh, '<', 'data.json' or die "Can't open file: $!";
local $/; # Slurp mode
my $json_text = <$fh>;
close $fh;
my $data = decode_json($json_text);
print "User: $data->{name}\n";
π Sending JSON in a Perl API (PSGI/Plack)
use Plack::Request;
use JSON;
my $app = sub {
my $req = Plack::Request->new(shift);
my %data = (
message => "Hello from Perl API",
status => "ok"
);
my $res = $req->new_response(200);
$res->content_type('application/json');
$res->body(encode_json(\%data));
return $res->finalize;
};
βοΈ Sends a JSON response from a Perl-based API service.
π§ Common JSON Modules in Perl
| Module | Description |
|---|---|
JSON | Pure Perl encoder/decoder (default) |
JSON::XS | Fast, written in C |
Cpanel::JSON::XS | Drop-in replacement, faster, RFC-compliant |
π‘οΈ Best Practices for JSON in Perl
| Practice | Reason |
|---|---|
Always use encode_json() and decode_json() | Ensures safe serialization |
| Use hash/array references | JSON functions expect references |
| Check for malformed JSON | Prevent crashes with eval or error checks |
Use use strict; use warnings; | Keeps code clean and error-free |
π Summary β Recap & Next Steps
JSON with Perl is straightforward and powerful, especially when building CLI tools, RESTful APIs, or server-side scripts that require reliable data exchange.
π Key Takeaways:
- Use
encode_json()anddecode_json()for conversion - JSON data in Perl is represented as references
- Supports API responses, config files, and data logs
- Perl modules like
JSON::XSoffer performance benefits
βοΈ Real-world use:
Used in API development, data processing scripts, config management, and web backend services.
β FAQ β JSON in Perl
β Which JSON module should I use in Perl?
β
Use JSON::XS or Cpanel::JSON::XS for performance; JSON for portability.
β How do I convert a Perl array to JSON?
β
Use encode_json(\@array) to get a JSON array string.
β What does decode_json() return?
β
A reference to a hash or array, depending on the JSON structure.
β Can I use JSON in CGI Perl scripts?
β
Yes. Just set the content type to application/json and use encode_json().
Share Now :
