1

I am new to perl, Need a help in decoding json.

json output from url

{
    "result": [
        {
            "manager": {
                "value": "testvalue",
                "data": "testdata"
            }
        }
    ]
}
my $decoded = decode_json($client->responseContent());

print  Dumper($decoded->{'result'}['manager']);

This will print below.

$VAR1 = {
      'manager' => {
                     'value' => 'testvalue',
                     'data' => 'testdata'
                   }
    };

How can I get the testvalue in a variable.? I tried different ways but not able to crack

2
  • 1
    When you post questions, please include the code that you have tried, not just "I tried different ways". Commented Aug 9, 2021 at 16:25
  • 3
    print $decoded->{result}[0]{manager}{value} Commented Aug 9, 2021 at 16:34

2 Answers 2

4

This statement is wrong:

print  Dumper($decoded->{'result'}['manager']);

Because you do not have warnings turned on, which is a very bad thing, you do not get the error

Argument "manager" isn't numeric in array element at ...

You are using square brackets [ .. ] which means Perl expects what is inside it to be an array index, a number. You put a string. Perl will automatically try to cast the string to a number, but will warn you about it. Unless you foolishly have warnings turned off. Any string that does not begin with a number is cast to 0, so it will try and access array element 0, like this:

print  Dumper($decoded->{'result'}[0]);

Which is exactly what you see in the Dumper output:

$VAR1 = {
      'manager' => {
                     'value' => 'testvalue',
                     'data' => 'testdata'
                   }
    };

What you need to do is to get the value from the key 'value' inside the hash value for key 'manager', inside the first array element of the hash stored in the key 'result':

print $decoded->{'result'}[0]{'manager'}{'value'}

You can easily traverse the json structure to see the Perl structure, which looks similar.

{                                       # hash
    "result": [                         # key "result", stores array [
        {                                 # hash
            "manager": {                  # key "manager", stores hash {
                "value": "testvalue",        # key "value", value "testvalue"
                "data": "testdata"           # key "data", value "testdata"
            }
        }
    ]
}

Always put this in your Perl code:

use strict;
use warnings;

To avoid making simple mistakes or hide critical errors.

Sign up to request clarification or add additional context in comments.

Comments

0
use strict;
use warnings;
use Carp;
use English qw( -no_match_vars );

# ...

my $decoded;

my $status = eval {
    $decoded = decode_json( $client->responseContent() );
    1;
};

if ( !$status || $EVAL_ERROR ) {
    croak 'some goes wrong';
}

if (   ref $decoded ne ref {}
    || ref $decoded->{result} ne ref []
    || !scalar @{ $decoded->{result} }
    || ref $decoded->{result}->[0] ne ref            {}
    || ref $decoded->{result}->[0]->{manager} ne ref {}
    || !exists $decoded->{result}->[0]->{manager}->{value} )
{
    croak 'wrong structure';
}

my $manager = $decoded->{result}->[0]->{manager}->{value};

2 Comments

This answer would be much better if you explained how it answers the question and what it does (similarly to what TLP does in his answer: it's not just a dump of code).
you should always make checks to make sure that the data corresponds to what you are asking for. Otherwise, the code can shoot out in the most unexpected way at the most inopportune moment.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.