tags:

views:

90

answers:

1

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."
        }
}
+2  A: 

I'm not certain I understand how you want the JSON output to be sorted -- aside from sorting by hash key. If that's all you want, just pass the canonical method a true argument.

use strict;
use warnings;

use JSON::PP;

# A simple hash-of-hashes for exploration.
my $h = {
    Z => { c => 1, d => 2 },
    A => { a => 3, r => 4 },
    B => { c => 5, x => 6 },
    S => { q => 7, d => 8 },
};

my $js = JSON::PP->new;
$js->canonical(1);

my $output = $js->encode($h);
print $output;

If you do use the sort_by method, it does not make sense to use $_ within the sort block: what would it represent? It was not clear from the documentation what arguments the sort_by code will receive. Using Data::Dumper like this:

use Data::Dumper qw(Dumper);

my $sorter = sub {
    # See what's going on.
    print "$JSON::PP::a cmp $JSON::PP::b\n";
    print Dumper(\@_, $_);
    <STDIN>;

    # Sort hash keys alphabetically.
    $JSON::PP::a cmp $JSON::PP::b;
};

my $output = $js->sort_by($sorter)->encode($h);

You can infer that sort_by works like this: (1) it receives two arguments, the JSON::PP object and the hash ref currently being worked with; and (2) the $JSON::PP::a and $JSON::PP::b variables hold the hash keys being compared. But note that the hash ref refers to the JSON output as it is being built from the leaf nodes upward. It does not refer to your original data structure. This would seem to make the task of writing a comparator a bit trickier. Good luck.

my $sorter = sub {
    my ($json_pp_object, $hash_ref) = @_;

    # Write your own comparator here.
};

my $output = $js->sort_by($sorter)->encode($h);
FM
Intended sorting: First alphabetically by hash key, Second alphabetically by first hashref subkey. Maybe alternatively,(second), numerically by value of "pos" (after figuring out how sorting works on nested structures).
knb