$JSON::KeySort $json->canonical->encode($perl_scalar) This is the ascii sort. If you want to use with your own sort routine, check the sort_by method. (Only with JSON::PP, even if -support_by_pp is used currently.) $json->sort_by($sort_routine_ref)->encode($perl_scalar) $json->sort_by(sub { $JSON::PP::a <=> $JSON::PP::b })->encode($perl_scalar) Can't access $a and $b but $JSON::PP::a and $JSON::PP::b.