Perl: How can I sort a complex structure using JSON::PP ?
From the JSON Documentation:
As the sorting routine runs in the JSON::PP scope, the given subroutine name and the special variables $a, $b will begin 'JSON::PP::'.
Here is my attempt, does not seem to work
open my $fh, ">", $file or warn " exportAsJSON: can't open file: '$file': $!";
print $fh $coder->sort_by(sub {$_->{column_def}->{$JSON::PP::a} cmp $_->{column_def}->{$JSON::PP::b} } )->encode(\%json);
close $fh;
I want to sort by key, then the column_def attribute on the attribute key below "column_def", i.e. density, depth_in_m, mag_sus :
{
"column_def":
{
"depth_in_m":
{
"names":"depth_in_m",
"pos":"0"
},
"mag_sus":
{
"names":
{
"A_ALIAS":"Mag-Sus.",
"A_DESC":"magnetic susceptibility in SI",
"ATTRIBUTE":"MAG_SUS"
},
"pos":"2"
},
"density":
{
"names":
{
"A_ALIAS":"Density",
"A_DESC":"density in gm\/cc",
"ATTRIBUTE":"DENSITY"
},
"pos":"1"
}
},
"data":
{
"depth_in_m":"14.635",
"mag_sus":"n.a.",
"density":"n.a."
}
}