in reply to Re^3: Outputting JSON with sorted names/keys (Updated)
in thread Outputting JSON with sorted names/keys

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.

Replies are listed 'Best First'.
Re^5: Outputting JSON with sorted names/keys (Updated)
by pryrt (Abbot) on Jun 20, 2023 at 16:50 UTC
    Use of canonical fixed my problem

    I'm glad that the alphanumeric sort of canonical worked for you. However, use of canonical is not sufficient for the original problem that I asked: I didn't just want it sorted; I wanted it sorted in a non-traditional sort order (that is, in an order that I defined). So canonical sort is possible in both, but the arbitrary sort_by mechanism is only available in JSON::PP .