json_pp returning randomly ordered data

I noticed that json_pp was returning data randomly. This is due to a perl internal (

Perl hash references become JSON objects. As there is no inherent ordering in hash keys (or JSON objects), they will usually be encoded in a pseudo-random order. JSON::XS can optionally sort the hash keys (determined by the canonical flag), so the same datastructure will serialise to the same JSON text (given same settings and version of JSON::XS), but this incurs a runtime overhead and is only rarely useful, e.g. when you want to compare some JSON text against another for equality.

To overcome this you can include these arguments to json_pp:

json_pp --json_opt=canonical,pretty < json.txt


Leave a Reply

Your email address will not be published. Required fields are marked *

Comment replies are not available offline

Thanks! I used json_pp to make API output readable, but got file differences even if the values where the same. Not anymore!

on September 7, 2018 at 8:06 am Reply |

Thanks! Just started using json_pp to parse REST data and I noticed it looked a little wacky.

on January 23, 2018 at 2:14 pm Reply |