Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

How do I Pull data out of Array inside of Hash

by nachtmsk (Acolyte)
on Jan 25, 2020 at 00:33 UTC ( [id://11111848]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I am trying to pull JSON data from an online source.

I was able to get the data and store it into $datahash.

I dereferenced that into %hashdata

Then ran through the keys. Output below.

The key named "value" is an array. I need to run through that data in that array and work with it. It is the first value in the data section I printed out below the code.

My question is how can I get the data out of that ARRAY that is in the hash %hashdata.

Thanks very much for any help.

Mike
use LWP::Simple 'get'; use Data::Dumper; use Mojo::JSON qw(decode_json encode_json); use constant URL => 'https://api.bridgedataoutput.com/api/v2/OData/tes +t/test?access_token=xxxxxxxxxxxxxx'; my $json = get URL or die "Unable to get JSON data"; my $datahash = decode_json $json; %hashdata = %$datahash; #print Dumper(%hashdata); #print $hashdata{'value'}; ## Array that holds the data foreach $key (keys %hashdata) { $value = $hashdata{$key}; print "Key $key Value $value\n\n\n"; }
Data returned =============

Key value Value ARRAY(0x5612912d40c8) ## NEED to get into this Array and get data out.

Key @odata.context Value https://rets.io/api/v2/OData/test/

Key @odata.count Value 10006

Key @odata.nextLink Value https://rets.io/api/v2/OData/test/test?access_token=xxxxxxxxx

Replies are listed 'Best First'.
Re: How do I Pull data out of Array inside of Hash
by choroba (Cardinal) on Jan 25, 2020 at 00:40 UTC
    my $value = $hashdata{$key}; my @array = @$value; # The whole array. my $first = $value->[0]; # The first element. my $last = $value->[-1]; # The last element.

    $value is an array reference. To get to the array, we need to de-reference it.

    Seems like you should read perlref and perlreftut.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: How do I Pull data out of Array inside of Hash
by AnomalousMonk (Archbishop) on Jan 25, 2020 at 03:55 UTC
Re: How do I Pull data out of Array inside of Hash
by Anonymous Monk on Jan 25, 2020 at 07:29 UTC

    If you're using Mojo, use ojo :)

    use strict; use warnings; use ojo qw/ r g j /; my $g = g( URL() ); print join "\n\n", $g, r( $g ), r( j( $g->body ) ); __END__ Mojo::Message::Response=HASH(0x1dec95c) bless( { "code" => 403, "content" => bless( { "asset" => bless( { "auto_upgrade" => 1, "content" => "{\"error\":{\"code\":403,\"message\":\"Invalid acc +ess_token format\"}}", "max_memory_size" => 262144 }, 'Mojo::Asset::Memory' ), "auto_decompress" => 1, "auto_relax" => 1, "auto_upgrade" => 1, "body" => 4, "buffer" => "", "chunk_len" => 0, "chunk_state" => "finished", "events" => { "read" => [ sub { "DUMMY" } ] }, "gz" => bless( do{\(my $o = 31422964)}, 'Compress::Raw::Zlib::infl +ateStream' ), "header_size" => 997, "headers" => bless( { "cache" => [], "headers" => { "access-control-allow-credentials" => [ "true" ], "access-control-allow-headers" => [ "Accept, Content-Type, Origin, X-Requested-With, Authorizati +on" ], "access-control-allow-methods" => [ "GET, PUT, POST, DELETE, OPTIONS" ], "access-control-expose-headers" => [ "Retsly-Session" ], "cache-control" => [ "no-store, no-cache, must-revalidate, proxy-revalidate" ], "connection" => [ "keep-alive" ], "content-length" => [ 62 ], "content-type" => [ "application/json; charset=utf-8; ieee754compatible=undefine +d; odata.metadata=undefined; odata.streaming=undefined" ], "date" => [ "Sat, 25 Jan 2020 07:28:22 GMT" ], "expires" => [ 0 ], "odata-version" => [ "4.0" ], "pragma" => [ "no-cache" ], "referrer-policy" => [ "origin-when-cross-origin" ], "server" => [ "nginx/1.10.3 (Ubuntu)" ], "strict-transport-security" => [ "max-age=31536000" ], "surrogate-control" => [ "no-store" ], "x-content-type-options" => [ "nosniff" ], "x-download-options" => [ "noopen" ], "x-frame-options" => [ "SAMEORIGIN" ], "x-xss-protection" => [ "1; mode=block" ] }, "max_line_size" => 8192, "max_lines" => 100, "names" => { "access-control-allow-credentials" => "Access-Control-Allow-Cr +edentials", "access-control-allow-headers" => "Access-Control-Allow-Header +s", "access-control-allow-methods" => "Access-Control-Allow-Method +s", "access-control-expose-headers" => "Access-Control-Expose-Head +ers", "odata-version" => "OData-Version", "pragma" => "Pragma", "referrer-policy" => "Referrer-Policy", "requestid" => "requestId", "surrogate-control" => "Surrogate-Control", "x-content-type-options" => "X-Content-Type-Options", "x-download-options" => "X-Download-Options", "x-frame-options" => "X-Frame-Options", "x-xss-protection" => "X-XSS-Protection" }, "state" => "finished" }, 'Mojo::Headers' ), "max_buffer_size" => 262144, "post_buffer" => "", "raw_size" => 1087, "read" => $VAR1->{"content"}{"events"}{"read"}[0], "real_size" => 79, "size" => 79, "state" => "finished" }, 'Mojo::Content::Single' ), "error" => { "code" => 403, "message" => "Forbidden" }, "events" => {}, "finished" => 3, "max_line_size" => 8192, "max_message_size" => 2147483648, "message" => "Forbidden", "raw_size" => 1111, "state" => "finished", "version" => "1.1" }, 'Mojo::Message::Response' ) { "error" => { "code" => 403, "message" => "Invalid access_token format" } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11111848]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-23 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found