I have a file that's almost YAML, with the exception of duplicate keys. Is there a existing method to convert duplicate keys into arrays?
My code looks like:
use Data::Dumper;
use YAML::XS;
local $YAML::XS::ForbidDuplicateKeys = 1;
my $x = YAML::XS::Load($yaml_string);
print Dumper $x;
Sample input file:
info:
family: skywalker
person:
id: 1
status: active
details:
gender: male
name:
first: Anakin
last: Skywalker
children: Leia
children: Han Solo
person:
id: 58
status: active
details:
gender: female
name:
first: Padme
last: Amidala
children: Leia
children: Han Solo
Desired input file:
info:
family: skywalker
person:
-
id: 1
status: active
details:
gender: male
name:
first: Anakin
last: Skywalker
children:
- Leia
- Han Solo
-
id: 58
status: active
details:
gender: female
name:
first: Padme
last: Amidala
children:
- Leia
- Han Solo
The specification for YAML mappings says:
The content of a mapping node is an unordered set of key/value node pairs, with the restriction that each of the keys is unique.
Your keys are not unique and therefore you do not have a valid YAML (as you already know - "I have a file that's almost YAML"). Therefore, you should not expect to be able to parse that file with a YAML parser.
I guess you would need to write some kind of parser for your "almost YAML" which produces a Perl data structure in the format that you want. You could then use a standard YAML library to convert that data structure to YAML.