Thanks! By using Tie::Hash::MultiValueOrdered to create the initial hash, and then JSON::MultiValueOrdered to serialize it into JSON, the serialized output was in the right order.

use warnings; use strict; use autodie; use JSON; use Data::Dumper; 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->e +ncode( $dat ); print 'My Order => ', my $pryrtstr = manual_ordered_json( $dat, \@orde +r ); { # [tobyink][id://11111911] use JSON::MultiValueOrdered; use Tie::Hash::MultiValueOrdered; my $tied = tie my %hash, 'Tie::Hash::MultiValueOrdered'; $hash{id} = 'a'; $hash{disp} = 'a'; $hash{ver} = 'a'; $hash{auth} = 'a'; #my $d2 = [ {%hash}, {%hash} ]; # didn't maintain order -- oh, + right, because that made a normal hashref from an ordered hash; duh! my $d2 = [ \%hash, \%hash ]; # does maintain order print 'tobyink => ', my $tobyinkstr = JSON::MultiValueOrdered->new +(pretty=>1)->encode($d2); use Test::More tests => 1; $tobyinkstr =~ s/\s//g; $pryrtstr =~ s/\s//g; is_deeply $tobyinkstr, $pryrtstr, 'serialization match'; done_testing; } 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} // '< +undef>'; $out .= ',' if $j < $#ordr; $out .= "\n"; } $out .= " }"; $out .= "," if $i < $#list; $out .= "\n"; } $out .= "]\n"; return $out; }

(As you can tell from my commented-out line, I had a brain failure in my first attempt, and converted the ordered hash into a plain hashref, thus losing the original order. Once I realized that, I got it working.)


In reply to Re^2: Outputting JSON with sorted names/keys by pryrt
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.