How to convert JSON to YAML in Perl?
Question
How to convert JSON to YAML in Perl?
Converting JSON data to YAML in Perl is a common task when working with different data serialization formats. JSON (JavaScript Object Notation) and YAML (YAML Ain't Markup Language) are both human-readable formats for structured data, but YAML offers more concise syntax and is often preferred for configuration files.
To convert JSON to YAML in Perl, you typically parse the JSON string into a native Perl data structure (hashes, arrays, scalars) using a JSON module, then serialize that data structure into YAML using a YAML module.
Core Concepts
JSONparsing converts a JSON string into Perl data structures, respecting scalars, arrays, hashes, and nested combinations.YAMLserialization takes Perl data structures and writes them out as YAML formatted text.- Perl sigils (
$,@,%) represent scalars, arrays, and hashes respectively, which map well to JSON/YAML types. - TMTOWTDI ("There's More Than One Way To Do It") is demonstrated here by the wide range of serialization modules available on CPAN.
Recommended Modules
JSON: Core module for JSON encoding/decoding. Works well for parsing JSON strings.YAML::XSorYAML: For producing YAML output.YAML::XSis faster and more robust but requires XS (compiled) components,YAMLis pure Perl with fewer dependencies.
Note: Neither YAML nor YAML::XS comes with core Perl, but they are very common and fast to install from CPAN. If strictly avoiding non-core modules, YAML output is challenging natively. In practice, these modules are the de facto standard.
Runnable Example
This script takes a JSON string, decodes it to a Perl data structure, then outputs equivalent YAML:
use strict;
use warnings;
use JSON;
use YAML::XS;
# Sample JSON input (could come from anywhere)
my $json_text = q|
{
"name": "Alice",
"age": 30,
"languages": ["Perl", "Python", "JavaScript"],
"details": {
"occupation": "Developer",
"active": true
}
}
|;
# Decode JSON string into Perl data structure
my $data = decode_json($json_text);
# Encode Perl data structure into YAML string
my $yaml_text = Dump($data);
print "YAML output:\n";
print $yaml_text;
What is happening here?
decode_jsonturns the JSON text into Perl native data (hashref with nested arrayrefs and hashrefs).DumpfromYAML::XSconverts the Perl data to YAML format, preserving the structure.- Finally the YAML is printed to
STDOUT.
Common Pitfalls
- Character encoding: JSON strings are UTF-8 encoded. Ensure Perl handles encoding properly, especially when reading from files.
- Module dependencies: While JSON is included in core Perl 5.14+,
YAML::XSis not, so it must be installed via CPAN. - Data type differences: JSON booleans become Perl scalars with a true/false value; YAML interprets these naturally but beware of scalar vs reference confusion.
- Ref vs Scalar context:
decode_jsonreturns references by default, whichDumpexpects.
Summary
Converting JSON to YAML in Perl is straightforward by leveraging JSON parsing and YAML serialization modules. This approach lets you work with flexible, native Perl data structures before dumping to YAML. This pattern can be adapted for file input/output or web APIs that require different data formats.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 24ms
YAML output:
---
age: 30
details:
active: !!perl/scalar:JSON::PP::Boolean 1
occupation: Developer
languages:
- Perl
- Python
- JavaScript
name: Alice
(empty)