frank1 has asked for the wisdom of the Perl Monks concerning the following question:

am trying to get something in json msg and extract it

i want to get the last response area

{ "end": 2, "events": [ { "request": { "timeout": "55", } }, "response": { "response_code": 201, } }, { "request": { "method": "POST", "parameters": { "api_version": "2010-04-01", } }, "response": { "response_code": 200, } }, { "request": { "method": "POST", "parameters": { "api_version": "2010-04-01" } }, "response": { "response_code": 200 #i want to get this } } ] }
my $Msg = JSON::XS->new->decode ($res->content); my $Getinfo = $Msg->[events]->{response}->{response_code}; print "$Getinfo"; #prints 200

Replies are listed 'Best First'.
Re: Extract json data
by Arunbear (Prior) on Dec 23, 2024 at 19:24 UTC
    The last element of an array can be accessed using the index -1, so you'd need
    my $Getinfo = $Msg->{events}[-1]{response}{response_code};

      thank you