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

I'm stuck trying to access the values from the Tumblr api.. I'm something of a newbie to perl (or any language) so this one is baffling me although I'm sure it's a simple thing.. is it a harshref? array ref? array of hashes, hash of arrays? something else? Nothing I've tried so far returns anything useful.

For example I get errors like this when trying to access just the 'meta' fields..

Can't use string ("{"meta":{"status":200,"msg":"OK"") as an ARRAY ref while "strict refs" in use

What I really need is to get all the fields from the 'response' bit which presumably requires looping on something. Any help to resolve this would be gratefully received, before I lose what is left of my sanity.. ;)

$VAR1 = '{"meta":{"status":200,"msg":"OK"},"response":[{"blog_name":"w +mr101","id":69895404084,"post_url":"http:\\/\\/wmr101.tumblr.com\\/po +st\\/69895404084\\/while-read-stdin-2048-a-29-b-73-c-142-t","slug":"w +hile-read-stdin-2048-a-29-b-73-c-142-t","type":"quote","date":"2013-1 +2-13 18:18:57 GMT","timestamp":1386958737,"state":"published","format +":"html","reblog_key":"KEalDhb2","tags": ["perl"],"short_url":"http:\\/\\/tmblr.co\\/ZQJ0Ox1165peq","highlighte +d":[],"note_count":0,"text":"$_=’while(read+STDIN,$_,2048){$a=29;$b=7 +3;$c=142;$t=255;@t=map{$_%16or$t^=$c^=($m=(11,10,116,100,11,122,20,10 +0)[$_\\/16%8])&110;$t^=(72,@z=(64,72,$a^=12*($_%16-2?0:$m&17)),$b^=$_ +%64?12:0,@z)[$_%8]}(16..271);if((@a=unx”C*”,$_)[20]&48){$h==5;$_==unx +b24,join”“,@b=map{xB8,unxb8,chr($_^$a[—$h+84])}@ARGV;s\\/…$\\/1$&\\/; +$d=unxV,xb25,$_;$e=256|(ord$b[4])>8^($f=$t&($d»12^$d»4^$d^$d\\/8))>8^ +($t&($g=($q=$e»14&7^$e)^$q*8^$q>=8)+=$f+(~$g&$t))for@a[128..$#a]}prin +t+x”C*”,@a}’;s\\/x\\/pack+\\/g;eval","source":""}, {"blog_name":"a0001","id":69691252013,"post_url":"http:\\/\\/a0001.tum +blr.com\\/post\\/69691252013\\/perl","slug":"perl","type":"text","dat +e":"2013-12-11 14:48:03 GMT","timestamp":1386773283,"state":"publishe +d","format":"html","reblog_key":"Lhh9Osdt","tags":["perl"],"short_url +":"http:\\/\\/tmblr.co\\/ZSfAvp10vx1qj","highlighted":[],"note_count" +:0,"title":"perl\\u3067\\u30d5\\u30a1\\u30a4\\u30eb\\u3092\\u8aad\\u3 +07f\\u8fbc\\u3080","body":"\\u003Cp\\u003E\\u30d5\\u30a1\\u30a4\\u30e +b\\u30aa\\u30fc\\u30d7\\u30f3\\u3001\\u3088\\u304f\\u4f7f\\u3046\\u30 +6e\\u3067\\u66f8\\u3044\\u3066\\u307f\\u307e\\u3057\\u305f\\u3002\\u0 +03C\\/p\\u003E\\n\\u003Cpre class=\\u0022brush:perl;\\u0022\\u003E op +en $FH, \\u0022hoge.txt\\u0022 or die $!;\\n while ( readline $FH ) { +\\n # \\u51e6\\u7406 \\n }\\n clise $FH;\\n\\u003C\\/pre\\u003E\\n\\u +003Cp\\u003Eor\\u6f14\\u7b97\\u5b50\\u306f\\u5de6\\u8fba\\u3092\\u8a5 +5\\u4fa1\\u3057\\u3066\\u3002\\u7d50\\u679c\\u304c\\u507d\\u306e\\u58 +34\\u5408\\u306f\\u003C\\/p\\u003E\\n\\u003Cp\\u003E\\u53f3\\u8fba\\u +3092\\u5b9f\\u884c\\u3057\\u307e\\u3059\\u3002\\u3053\\u306e\\u5834\\ +u5408\\u306f\\u30d5\\u30a1\\u30a4\\u30eb\\u30aa\\u30fc\\u30d7\\u30f3\ +\u304c\\u5931\\u6557\\u3059\\u308b\\u3068\\u003C\\/p\\u003E\\n\\u003C +p\\u003Eundef\\u304c\\u8fd4\\u308a\\u3001undef\\u306ffalse\\uff08\\u5 +07d\\uff09\\u6271\\u3044\\u306a\\u306e\\u3067\\u003C\\/p\\u003E\\n\\u +003Cp\\u003Edie\\u95a2\\u6570\\u3092\\u5b9f\\u884c\\u3057\\u3066\\u30 +d7\\u30ed\\u30b0\\u30e9\\u30e0\\u304c\\u7d42\\u4e86\\u3057\\u307e\\u3 +059\\u3002\\u003C\\/p\\u003E\\n\\u003Cp\\u003E\\u305d\\u306e\\u3068\\ +u304d$!\\uff1d\\u30a8\\u30e9\\u30fc\\u30e1\\u30c3\\u30bb\\u30fc\\u30b +8\\u304c\\u6a19\\u6e96\\u51fa\\u529b\\u306b\\u8868\\u793a\\u3055\\u30 +8c\\u307e\\u3059\\u3002\\u003C\\/p\\u003E\\n\\u003Cp\\u003E\\u3081\\u +3067\\u305f\\u304f\\u6210\\u529f\\u3057\\u305f\\u3068\\u304d\\u306fwh +ile( readline $FH )\\u306b\\u9032\\u307f\\u307e\\u3059\\u3002\\u003C\\/p\\u003E\\n\\u00 +3Cp\\u003Ereadline\\u3067\\u30d5\\u30a1\\u30a4\\u30eb\\u306e\\u5185\\ +u5bb9\\u3092\\u4e00\\u884c\\u305a\\u3064\\u53d6\\u5f97\\u3057\\u3066\ +\u003C\\/p\\u003E\\n\\u003Cp\\u003E\\u4f55\\u304b\\u3057\\u3089\\u51e +6\\u7406\\u3067\\u304d\\u307e\\u3059\\u3002\\u53d6\\u5f97\\u3057\\u30 +5f\\u5185\\u5bb9\\u306f$_\\u306b\\u5165\\u3063\\u3066\\u307e\\u3059\\ +u30fc\\u3002\\u003C\\/p\\u003E\\n\\u003Cp\\u003E\\u003Ca href=\\u0022 +http:\\/\\/www.perlplus.jp\\/perl\\/file\\/index3.html\\u0022 target= +\\u0022_blank\\u0022\\u003Ehttp:\\/\\/www.perlplus.jp\\/perl\\/file\\ +/index3.html\\u003C\\/a\\u003E\\u003C\\/p\\u003E"}, etc.

Thanks!

Replies are listed 'Best First'.
Re: Extracting data from api response..
by choroba (Cardinal) on Dec 14, 2013 at 19:32 UTC
    Looks like JSON.
    use JSON; use Data::Dumper; my $json = '...'; # Put your VAR1 here. my $p = from_json($json); print Dumper $p;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Extracting data from api response..
by roboticus (Chancellor) on Dec 14, 2013 at 23:51 UTC

    JackPerl:

    As choroba noted, it looks like JSON. I took your sample data, closed the open structures (]}) and slurped it in using JSON. After looking at the data structure, I also added a little code to show how to access some of the individual elements.

    #!/usr/bin/perl use strict; use warnings; use JSON; use Data::Dumper; # Read the JSON string & clean it up a little. You don't need this pa +rt, it's # only here to turn your sample into a text string. my $txt = join("", map { s/\s+$//; s{\\\\}{\\}g; $_ } <DATA>); #print $txt,"\n"; #print "-"x50,"\n"; # Create JSON parser and decode the string my $json = JSON->new->allow_nonref; $json = $json->relaxed(1); my $r = $json->decode($txt); # Dump the decoded data to the screen so we can see how to access the +bits #print Dumper($r); # Dump some of the bits: print "RESPONSE STATUS: ", $r->{meta}{status}, "=", $r->{meta}{msg}, " +\n"; for my $item (@{ $r->{response} }) { print "BLOG NAME: ", $item->{blog_name}, ", DATE:", $item->{date}, + "\n"; } __DATA__ {"meta":{"status":200,"msg":"OK"},"response":[{"blog_name":"wmr101","i +d":698 95404084,"post_url":"http:\\/\\/wmr101.tumblr.com\\/post\\/69895404084 +\\/while-read- stdin-2048-a-29-b-73-c-142-t","slug":"while-read-stdin-2048-a-29-b-73- +c-142-t","type ":"quote","date":"2013-12-13 18:18:57 GMT","timestamp":1386958737,"sta +te":"published ","format":"html","reblog_key":"KEalDhb2","tags": ["perl"],"short_url":"http:\\/\\/tmblr.co\\/ZQJ0Ox1165peq","highlight +ed":[],"note_cou nt":0,"text":"$_=’while(read+STDIN,$_,2048){$a=29;$b=73;$c=142;$t=255; +@t=map{$_%16or $t^=$c^=($m=(11,10,116,100,11,122,20,100)[$_\\/16%8])&110;$t^=(72,@z=( +64,72,$a^=12*( $_%16-2?0:$m&17)),$b^=$_%64?12:0,@z)[$_%8]}(16..271);if((@a=unx”C*”,$_ +)[20]&48){$h== 5;$_==unxb24,join”“,@b=map{xB8,unxb8,chr($_^$a[—$h+84])}@ARGV;s\\/…$\\ +/1$&\\/;$d=unx V,xb25,$_;$e=256|(ord$b[4])>8^($f=$t&($d»12^$d»4^$d^$d\\/8))>8^($t&($g +=($q=$e»14&7^$ e)^$q*8^$q>=8)+=$f+(~$g&$t))for@a[128..$#a]}print+x”C*”,@a}’;s\\/x\\/p +ack+\\/g;eval" ,"source":""}, {"blog_name":"a0001","id":69691252013,"post_url":"http:\\/\\/a0001.tu +mblr.com\\/post\ \/69691252013\\/perl","slug":"perl","type":"text","date":"2013-12-11 1 +4:48:03 GMT"," timestamp":1386773283,"state":"published","format":"html","reblog_key" +:"Lhh9Osdt","t ags":["perl"],"short_url":"http:\\/\\/tmblr.co\\/ZSfAvp10vx1qj","highl +ighted":[],"no te_count":0,"title":"perl\\u3067\\u30d5\\u30a1\\u30a4\\u30eb\\u3092\\u +8aad\\u307f\\u 8fbc\\u3080","body":"\\u003Cp\\u003E\\u30d5\\u30a1\\u30a4\\u30eb\\u30a +a\\u30fc\\u30d 7\\u30f3\\u3001\\u3088\\u304f\\u4f7f\\u3046\\u306e\\u3067\\u66f8\\u304 +4\\u3066\\u307 f\\u307e\\u3057\\u305f\\u3002\\u003C\\/p\\u003E\\n\\u003Cpre class=\\u +0022brush:perl ;\\u0022\\u003E open $FH, \\u0022hoge.txt\\u0022 or die $!;\\n while ( + readline $FH ) {\\n # \\u51e6\\u7406 \\n }\\n clise $FH;\\n\\u003C\\/pre\\u003E\\n\ +\u003Cp\\u003E or\\u6f14\\u7b97\\u5b50\\u306f\\u5de6\\u8fba\\u3092\\u8a55\\u4fa1\\u30 +57\\u3066\\u30 02\\u7d50\\u679c\\u304c\\u507d\\u306e\\u5834\\u5408\\u306f\\u003C\\/p\ +\u003E\\n\\u00 3Cp\\u003E\\u53f3\\u8fba\\u3092\\u5b9f\\u884c\\u3057\\u307e\\u3059\\u3 +002\\u3053\\u3 06e\\u5834\\u5408\\u306f\\u30d5\\u30a1\\u30a4\\u30eb\\u30aa\\u30fc\\u3 +0d7\\u30f3\\u3 04c\\u5931\\u6557\\u3059\\u308b\\u3068\\u003C\\/p\\u003E\\n\\u003Cp\\u +003Eundef\\u30 4c\\u8fd4\\u308a\\u3001undef\\u306ffalse\\uff08\\u507d\\uff09\\u6271\\ +u3044\\u306a\\ u306e\\u3067\\u003C\\/p\\u003E\\n\\u003Cp\\u003Edie\\u95a2\\u6570\\u30 +92\\u5b9f\\u88 4c\\u3057\\u3066\\u30d7\\u30ed\\u30b0\\u30e9\\u30e0\\u304c\\u7d42\\u4e +86\\u3057\\u30 7e\\u3059\\u3002\\u003C\\/p\\u003E\\n\\u003Cp\\u003E\\u305d\\u306e\\u3 +068\\u304d$!\\ uff1d\\u30a8\\u30e9\\u30fc\\u30e1\\u30c3\\u30bb\\u30fc\\u30b8\\u304c\\ +u6a19\\u6e96\\ u51fa\\u529b\\u306b\\u8868\\u793a\\u3055\\u308c\\u307e\\u3059\\u3002\\ +u003C\\/p\\u00 3E\\n\\u003Cp\\u003E\\u3081\\u3067\\u305f\\u304f\\u6210\\u529f\\u3057\ +\u305f\\u3068\ \u304d\\u306fwhile( readline $FH )\\u306b\\u9032\\u307f\\u307e\\u3059\\u3002\\u003C\\/p\\u003E\\n\\u0 +03Cp\\u003Ereadl ine\\u3067\\u30d5\\u30a1\\u30a4\\u30eb\\u306e\\u5185\\u5bb9\\u3092\\u4 +e00\\u884c\\u3 05a\\u3064\\u53d6\\u5f97\\u3057\\u3066\\u003C\\/p\\u003E\\n\\u003Cp\\u +003E\\u4f55\\u 304b\\u3057\\u3089\\u51e6\\u7406\\u3067\\u304d\\u307e\\u3059\\u3002\\u +53d6\\u5f97\\u 3057\\u305f\\u5185\\u5bb9\\u306f$_\\u306b\\u5165\\u3063\\u3066\\u307e\ +\u3059\\u30fc\ \u3002\\u003C\\/p\\u003E\\n\\u003Cp\\u003E\\u003Ca href=\\u0022http:\\ +/\\/www.perlpl us.jp\\/perl\\/file\\/index3.html\\u0022 target=\\u0022_blank\\u0022\\ +u003Ehttp:\\/\ \/www.perlplus.jp\\/perl\\/file\\/index3.html\\u003C\\/a\\u003E\\u003C +\\/p\\u003E"}, ]}

    When you run it, it gives:

    $ perl ex_json.pl RESPONSE STATUS: 200=OK BLOG NAME: wmr101, DATE:2013-12-13 18:18:57 GMT BLOG NAME: a0001, DATE:2013-12-11 14:48:03 GMT

    You'll notice a couple lines of code commented out. They're what I used when debugging the example.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Success! JSON did the trick, all works nicely now. Thanks!
Re: Extracting data from api response..
by Anonymous Monk on Dec 15, 2013 at 06:40 UTC

    I'm stuck trying to access the values from the Tumblr api.. I

    Which docs are you following (what link)?