I have an array of hashes that I would like to convert into an un-named JSON array.
If I have an array of hashes which I then try to encode to JSON as such:
my @labs = ();
push (@labs, {id=>'1', title=>'Lab1'});
push (@labs, {id=>'2', title=>'Lab2'});
my $json_text = to_json {\@labs}, {ascii=>1, pretty => 1};
then the resulting JSON looks like:
{
"ARRAY(0x358a18)" : null
}
when in fact I want it to look like:
[
{"title" : "Lab1", "id" : "1"},
{"title" : "Lab2", "id" : "2"}
]
arrayto the hash:to_json ({array => \@labs}or use as @MattJacob said:to_json \@labs, {ascii=>1, pretty => 1}\@labs- they're converting the array into an object@labsin an anonymous hash.use strictanduse warnings 'all'at thje top of every Perl program you write. That code gives Odd number of elements in anonymous hash because of{\@labs}