in reply to Outputting JSON with sorted names/keys

> Is there an option I'm not seeing in the JSON docs for altering the sort-order?

Are you aware of JSON::PP#sort_by ?

Not sure if I'm missing your point though...

Update

To elaborate more: JSON.pm has the choice between different backends which must implement it's interface.

But a backend can also provide more extended features.

See demo in my answer here.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

  • Comment on Re: Outputting JSON with sorted names/keys (Updated)

Replies are listed 'Best First'.
Re^2: Outputting JSON with sorted names/keys (POC)
by LanX (Saint) on Jan 26, 2020 at 03:51 UTC
    > Are you aware of JSON::PP#sort_by ?

    as a demo:

    (NB: I changed your indentation levels to make it identical, otherwise please uncomment the whitespace eraser prior to testing. )

    use warnings; use strict; use autodie; BEGIN { $ENV{PERL_JSON_BACKEND}='JSON::PP'; } use JSON; use Test::More; use 5.010; my @order = qw/id disp ver auth/; my $dat = [ ({ id => 'a', disp => 'a', ver => 'a', auth => 'a' })x2 ]; + # dummy data local $\ = "\n"; #print 'Unsorted => ', JSON->new->indent->space_after->encode( $dat ); #print 'Alpha Sorted => ', JSON->new->indent->space_after->canonical-> +encode( $dat ); print "Pryrt's Order => ", my $pryrt = manual_ordered_json( $dat, \@order ); # --- Sorting with JSON::PP my %ORDER; @ORDER{@order} = 1..@order; my $sort = sub { package JSON::PP; ($ORDER{$a} // 1e999) <=> ($ORDER{$b} // 1e999) or $a cmp $b }; print "LanX' Order => ", my $lanx = JSON->new->sort_by($sort)->indent->space_after->encode( + $dat ); # # --- erase whitespace diffs # s/\s//g for $pryrt, $lanx; is($lanx,$pryrt,"same JSON"); done_testing(); use Data::Dumper; sub manual_ordered_json{ my @list = @{$_[0]}; my @ordr = @{$_[1]}; my $out = "[\n"; for my $i ( 0 .. $#list ) { my $h = $list[$i]; $out .= " {\n"; for my $j ( 0 .. $#ordr ) { my $k = $ordr[$j]; next unless defined $k; next unless exists $h->{$k}; $out .= sprintf qq| "%s": "%s"|, $k, $h->{$k} // '<un +def>'; $out .= ',' if $j < $#ordr; $out .= "\n"; } $out .= " }"; $out .= "," if $i < $#list; $out .= "\n"; } $out .= "]\n"; return $out; }

    --->

    Pryrt's Order => [ { "id": "a", "disp": "a", "ver": "a", "auth": "a" }, { "id": "a", "disp": "a", "ver": "a", "auth": "a" } ] LanX' Order => [ { "id": "a", "disp": "a", "ver": "a", "auth": "a" }, { "id": "a", "disp": "a", "ver": "a", "auth": "a" } ] ok 1 - same JSON 1..1

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      >> Are you aware of JSON::PP#sort_by?

      Thanks! I knew that JSON used JSON::PP or JSON::XS under the hood, but I hadn't thought to go searching through the backend modules for features not advertised (edit: "...not advertised at the top level"). That's just what I was hoping would exist somewhere. And thanks for the example to go with it: works great for me.

        > Thanks! ... And thanks for the example to go with it: works great for me.

        Your welcome, I'm learning a lot from interesting questions.

        The code is mostly adopted from the perldoc though and could be written in a more generic way.

        I also like the fact that JSON::PP automatically detects hashes ordered via tie interface.

        > I knew that JSON used JSON::PP or JSON::XS under the hood,

        Not only under the hood, you can even use both modules in the same code, as long as you are restricting one of them to pure OO. =)

        use warnings; use strict; use autodie; use JSON::PP qw//; # don't export anything use JSON; use Test::More; use 5.010; my @order = qw/id disp ver auth/; my $dat = [ ({ id => 'a', disp => 'a', ver => 'a', auth => 'a' })x2 ]; + # dummy data local $\ = "\n"; #print 'Unsorted => ', JSON->new->indent->space_after->encode( $dat ); #print 'Alpha Sorted => ', JSON->new->indent->space_after->canonical-> +encode( $dat ); print "Pryrt's Order => ", my $pryrt = manual_ordered_json( $dat, \@order ); # --- Sorting with JSON::PP my %ORDER; @ORDER{@order} = 1 .. @order; my $sort = sub { package JSON::PP; ($ORDER{$a} // 1e999) <=> ($ORDER{$b} // 1e999) or $a cmp $b }; print "LanX' Order => ", my $lanx = JSON::PP->new->sort_by($sort)->indent->space_after->enc +ode( $dat ); # # --- erase whitespace diffs # s/\s//g for $pryrt, $lanx; is($lanx,$pryrt,"same JSON"); done_testing(); use Data::Dumper; sub manual_ordered_json{ my @list = @{$_[0]}; my @ordr = @{$_[1]}; my $out = "[\n"; for my $i ( 0 .. $#list ) { my $h = $list[$i]; $out .= " {\n"; for my $j ( 0 .. $#ordr ) { my $k = $ordr[$j]; next unless defined $k; next unless exists $h->{$k}; $out .= sprintf qq| "%s": "%s"|, $k, $h->{$k} // '<un +def>'; $out .= ',' if $j < $#ordr; $out .= "\n"; } $out .= " }"; $out .= "," if $i < $#list; $out .= "\n"; } $out .= "]\n"; return $out; }

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: Outputting JSON with sorted names/keys (Updated)
by bart (Canon) on Jun 16, 2023 at 12:03 UTC
    Rest assured: JSON::XS supports the same feature.
      Rest assured: JSON::XS supports the same feature.

      Interesting. Since you resurrected this thread, my curiosity has been piqued, and I couldn't remember whether I'd tried to do the same with JSON::XS three years ago or not.

      Searching through the JSON::XS codebase finds 0 instances of sort_by , so I am not sure what line of code in the source of JSON::XS could be implementing that function (though maybe there's an inheritance in the XS source, because I'm not sure what the equivalent of use/require/parent/base/@ISA are in XS). Because of that uncertainty, I took the working code that LanX had posted three years ago, and changed every instance of JSON::PP to JSON::XS . When I ran that, I got the message sort_by is not supported by JSON::XS. at C:\....\11111902.pl line 32. and the is() test failed, because the $lanx version was not sorted. Based on that experiment, I cannot see how to use sort_by with JSON::XS in an equivalent manner to how it's used with JSON::PP.

      Could you show an example of JSON::XS using sort_by (using the same data and structure as in the working example that LanX posted earlier)? And if it's just called something other than sort_by in the JSON::XS version, please let me know what the right name is (and whether or not it's documented in JSON::XS's POD). Because if it is possible, I'd like to see how. Thanks.

        I found this thread through Google because I recently had this problem.

        When I looked in the docs of JSON::XS I found pretty much the the same paragraph as in the docs of JSON::PP

        $json = $json->canonical ($enable)
        $enabled = $json->get_canonical
        If $enable is true (or missing), then the encode method will output JSON objects by sorting their keys. This is adding a comparatively high overhead.

        Use of canonical fixed my problem... and it will work in both.