Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl: how to sort a JSON structure on something other than "root" key attributes

Tags:

json

sorting

perl

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."
        }
}
like image 269
knb Avatar asked Aug 20 '10 14:08

knb


People also ask

How do I sort items in JSON?

Example-1: Sort JSON object using json.The value of the sort_keys argument of the dumps() function will require to set True to generate the sorted JSON objects from the array of JSON objects. Create a python file with the following script sort the JSON objects using json. dumps() function.

Can you sort a JSON object?

JSON return type is an array of objects. Hence sort method cannot be used directly to sort the array. However, we can use a comparer function as the argument of the 'sort' method to get the sorting implemented.


1 Answers

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);
like image 112
FMc Avatar answered Oct 07 '22 23:10

FMc