> 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


In reply to Re^2: Outputting JSON with sorted names/keys (POC) by LanX
in thread Outputting JSON with sorted names/keys by pryrt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.