Question

I got this output by decoding JSON data.

Now I want to reverse this data structure and print it using Perl.

Can anyone can help me with the code snippet below?

I have this data in one variable and I printed it. My output is as shown below

I have a hash of hashes that looks like this:

$VAR1 = {
    'Packet Loss to Source' => {
        '142' => {
            '161' => '0.000',
            '162' => '0.000',
            '141' => '0.000'
        }
    },
    'Packet Loss to Destination' => {
        '142' => {
            '161' => '0.000',
            '162' => '0.000',
            '141' => '0.000'
        }
    },
    'Average Jitter to Source' => {
        '142' => {
            '161' => '13.323',
            '162' => '37.003',
            '141' => '192.309'
        }
    },
    'Availability' => {
        '142' => {
            '161' => '0.000',
            '162' => '0.000',
            '141' => '1.042'
        }
    },
    'Average Round Trip Time' => {
        '142' => {
            '161' => '772.278',
            '162' => '389.566',
            '141' => '8557.511'
        }
    },
    'Average Jitter to Destination' => {

        '142' => {
            '161' => '13.323',
            '162' => '37.003',
            '141' => '192.309'
        }
    }
};

This is the code which I tried to get the output. I read the text file and get the JSON data, decode that and print it. I got the above output in variable $perl_obj.

#my $dirname = "/home/brix/ravikiran/doc/demo.txt";
my $dirname = "/home/brix/ravikiran/doc/JSONData.txt";

open HANDLE, $dirname;
my @raw_data = <HANDLE>;
my $json_text;
foreach my $row (@raw_data) {
    $json_text .= $row;
}
close(HANDLE);

print "$json_text;\n";
my $json = JSON::XS->new();
$json = json->pretty(1)->
    space_before(1)->
    space_after(1)->
    canonical(1)->
    allow_blessed(1)->
    convert_blessed(1);

my $perl_obj = $json->decode($json_text);
print STDOUT Dumper($perl_obj);
Was it helpful?

Solution

You probably don't want to reverse any of the hashes (at least in the sense Perl uses that keyword), because that involves swapping keys and values. What you probably want to do is change the order of the keys in your multi-level hash.

Here is how you could swap the outer two layers:

use warnings;
use strict;

my $new_obj;
foreach my $outer_key (keys %$perl_obj)
{
    foreach my $inner_key (keys %{$perl_obj->{$outer_key}})
    {
        $new_obj->{$inner_key}->{$outer_key} = 
            $perl_obj->{$outer_key}->{$inner_key};  
    }
}

use Data::Dumper;
print Dumper $new_obj;

Output:

$VAR1 = {
          '142' => {
                     'Packet Loss to Source' => {
                                                  '161' => '0.000',
                                                  '162' => '0.000',
                                                  '141' => '0.000'
                                                },
                     'Packet Loss to Destination' => {
                                                       '161' => '0.000',
                                                       '162' => '0.000',
                                                       '141' => '0.000'
                                                     },
          ...etc...

You could completely invert it like this:

my $new_obj;
foreach my $outer_key (keys %$perl_obj)
{
    foreach my $inner_key (keys %{$perl_obj->{$outer_key}})
    {
        foreach my $innest_key (keys %{$perl_obj->{$outer_key}->{$inner_key}})
        {
            $new_obj->{$innest_key}->{$inner_key}->{$outer_key} = 
                $perl_obj->{$outer_key}->{$inner_key}->{$innest_key};   
        }
    }
}

use Data::Dumper;
print Dumper $new_obj;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top