2

I have a HASH reference $job which has this data:

{
    "opstat"  : "ok",
    "response": {
                "group_id":23015,
                "order_id":"139370",
                "job_count":"10",
                "credits_used":"100.45",
                "currency":"USD"
                }
}

I want to print the hash value of "response" key. I tried doing this but did not work

print %{$job->{'response'}}

Edit

I do not want any formatting. I want to know how to access each element in the value of 'response' key.

1
  • Actually I think you do not have a "hash reference, but a JSON structure. Please show the code and any messages you get!
    – U. Windl
    Aug 1, 2023 at 8:18

4 Answers 4

3
I want to know how to access each element in the value of 'response' key.

By definition, you need some kind of loop. A foreach loop is typical, although you could also use map.

for my $key (keys %{$job->{response}}) {
   my  $val = $job->{response}{$key};
   print("$key: $val\n");  # Or whatever
}

or

my $response = $job->{response};
for my $key (keys %$response) {
   my  $val = $response->{$key};
   print("$key: $val\n");  # Or whatever
}
2

Try the following code, this is a real & complete script :

#!/usr/bin/env perl

use strict;
use warnings;

my $job = {
    'opstat' => 'ok',
    'response' => {
        'currency' => 'USD',
        'group_id' => ':23015',
        'job_count' => '10',
        'order_id' => '139370',
        'credits_used' => '100.45'
    }
};

foreach my $key (keys %{$job}) {
    print "key=$key|value=$job->{$key}\n";

    # Testing if "$job->{$key}" is a HASH ref
    # ...if yes, we iterate inside the HASH
    #  through the next level.
    if (ref($job->{$key}) eq "HASH") {
        foreach my $key2 (keys %{$job->{$key}}) {
            print "\tkey=$key2|value=$job->{$key}->{$key2}\n"; 
        }
    }
}

This is the output :

key=opstat|value=ok
key=response|value=HASH(0x1638998)
        key=currency|value=USD
        key=group_id|value=:23015
        key=order_id|value=139370
        key=job_count|value=10
        key=credits_used|value=100.45

If you want to access "group_id" key :

print $job->{response}->{group_id};

If you only want to access "response" HASH without testing anything :

foreach my $key (keys %{$job->{response}}) {
    print "key=$key|value=$job->{response}->{$key}\n";
}

or with with this while loop and each :

while (my ($key,$value) = each %{$job->{response}}){
    print "key=$key|value=$value\n";
}
1
use Data::Dumper;

print Dumper( $job->{response} );

or individually ...

print $job->{response}{group_id};
0

I'm not quite sure what you're trying to achieve; your code would print the contents of the inner hash without any formatting. If you want a formatted output, you'd have to use the Data::Dumper module:

use Data::Dumper;
my $job = {
  "opstat" => "ok",
  "response" => {
    "group_id":23015,
    "order_id":"139370",
    "job_count":"10",
    "credits_used":"100.45",
    "currency":"USD"
  }
};
print Dumper($job->{'response'});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.