Blog

json_pp returning randomly ordered data

I noticed that json_pp was returning data randomly. This is due to a perl internal (http://search.cpan.org/~mlehmann/JSON-XS-3.03/XS.pm)

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

1 Comment

Leave a Reply

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

Comment replies are not available offline

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 |