>Is there more elegant way to handle it?

The way you have it right now, as a recursive depth first (looks like) traversal of an abritrarily complex data structure, I am going to say no.

$_[0] = undef if (ref $r eq "HASH" && ! %$r) or (ref $r eq "ARRAY" && ! @$r);
The above code is simply the base case of your recursion, what allows it to unwind the call stack.

What is tripping me up is that I have no immediate idea what $$r{$k} is doing - is that some sort of generic reference? Double dollar signs are generally a red flag for me, so I can't really recognize when they might be doing something useful.

Do you have a snippet of JSON to share? I am really curious about your method of traversing the hash. I feel like there is a module on CPAN that allows more idiomatic interactions with your datastructure you call, $json.

Note: I always think myself later when I don't mix up what is $json (a string) and what is the deserialized reference:

my $reference_from_json = JSON->new->allow_nonref->relaxed->decode(do +{ local $/; <STDIN>; }); tidyup_json($refrence_from_json); print JSON->new->pretty->allow_nonref->canonical->encode($reference_fr +om_json);
>Beware that "true" and "false" jsonish data are references to blessed hashes.

Which one, JSON::PP::Boolean? In JSON::PP::Boolean::value_to_json, I see:

elsif( blessed($value) and $value->isa('JSON::PP::Boolean') ) +{ return $$value == 1 ? 'true' : 'false'; }
Indicates to me some implicit recognition of a TO_JSON method that the family of JSON modules support for serializing complex data structures containing blessed references into stringified JSON form.

In reply to Re: Tidy up json from nulls, empty arrays and hashes by perlfan
in thread 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.