I need to clean json data from nulls, empty arrays, empty hashes. Current solution:

#!/usr/bin/perl -CSDA # Removes empty arrays, empty objects, and nulls from json structure. use utf8; use Modern::Perl qw{2017}; use JSON; use Data::Dumper; sub tidyup_json { my ($r) = @_; if (ref $r eq "HASH") { for my $k (keys %$r) { tidyup_json($$r{$k}); delete $$r{$k} if (ref $$r{$k} eq "HASH" && ! %{$$r{$k}}) or (ref $$r{$k} eq "ARRAY" && ! @{$$r{$k}}) or ! defined $$r{$k}; } } elsif (ref $r eq "ARRAY") { tidyup_json($$r[$_]) for 0 .. @$r - 1; @$r = grep { not ( (ref $_ eq "HASH" && ! %$_) or (ref $_ eq "ARRAY" && ! @$_) or ! defined $_ ) } @$r; } # use "$_[0]", because "$_[0]" is an alias to real reference that +was passed to the function $_[0] = undef if (ref $r eq "HASH" && ! %$r) or (ref $r eq "ARRAY" && ! @$r); } my $json = JSON->new->allow_nonref->relaxed->decode(do { local $/; <ST +DIN>; }); tidyup_json($json); print JSON->new->pretty->allow_nonref->canonical->encode($json);

What's the problem? See the "$_[0]" at the end of function? This is an alias for passed reference of json data. If that json data is empty hash, then the data must become "undef". So I have to modify data outside of function -- using alias. Is there more elegant way to handle it?

Here are results:
# echo '{"empty inner hash": {}, "empty inner array": [], "some undef" +: null }' | ./tidyup_json null # echo '{"empty inner hash": {}, "empty inner array": [1], "some undef +": null }' | ./tidyup_json { "empty inner array" : [ 1 ] } # echo '{"empty inner hash": {}, "empty inner array": [1], "some undef +": 7 }' | ./tidyup_json { "empty inner array" : [ 1 ], "some undef" : 7 } # echo '{ }' | ./tidyup_json ## !!! important to return null null

PS. Beware that "true" and "false" jsonish data are references to blessed hashes.


In reply to Tidy up json from nulls, empty arrays and hashes by leszekdubiel

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.