in reply to Getting specific field values from JIRA rest/api output using JSON::XS and array of hashes

Have a look at perlreftut for an introduction to references.

I believe you are looking for:

$perl{'issues'}->[0]->{'fields'}->{'customfield_22181'}; $perl{'issues'}->[0]->{'fields'}->{'customfield_11150'}->{'value'}; # etc
#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; my %perl = ( 'issues' => [ { 'fields' => { 'customfield_22181' => 'Jira Testing', 'customfield_11150' => { 'value' => 'Yes', }, }, }, ], ); say $perl{'issues'}->[0]->{'fields'}->{'customfield_22181'}; say $perl{'issues'}->[0]->{'fields'}->{'customfield_11150'}->{'value'} +;
Output:
Jira Testing Yes

Hope this helps!


update: fixed typo
The way forward always starts with a minimal test.
  • Comment on Re: Getting specific field values from JIRA rest/api output using JSON::XS and array of hashes
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Getting specific field values from JIRA rest/api output using JSON::XS and array of hashes
by Anonymous Monk on Jan 20, 2016 at 15:59 UTC
    Hi, Now getting: Global symbol "%perl" requires explicit package name using $perl{issues} or $perl{issues}[0]{fields}. It seems that the type for the assignment of JSON::XS::decode_json $json is not a hash? Any other way to get the values? Thanks content of eg.json:
    {"expand":"names,schema","startAt":0,"maxResults":50,"total":1,"issues +":[{"expand":"operations,versionedRepresentations,editmeta,changelog, +transitions,renderedFields","id":"569825","self":"https://example.com +/rest/api/2/issue/569825","key":"TEST-10756","fields":{"summary":" Au +tomated Testing","issuetype":{"self":"https://example.com/rest/api/2/ +issuetype/10100","id":"10100","description":"","iconUrl":"https://exa +mple.com/secure/viewavatar?size=xsmall&avatarId=26414&avatarType=issu +etype","name":"Auto Testing","subtask":false,"avatarId":26414},"custo +mfield_11150":{"self":"https://example.com/rest/api/2/customFieldOpti +on/11347","value":"Yes","id":"11347"},"customfield_11780":[{"self":"h +ttps://example.com/rest/api/2/customFieldOption/12841","value":"Sunda +y","id":"12841"},{"self":"https://example.com/rest/api/2/customFieldO +ption/12842","value":"Monday","id":"12842"},{"self":"https://example. +com/rest/api/2/customFieldOption/12843","value":"Tuesday","id":"12843 +"},{"self":"https://example.com/rest/api/2/customFieldOption/12844"," +value":"Wednesday","id":"12844"},{"self":"https://example.com/rest/ap +i/2/customFieldOption/12845","value":"Thursday","id":"12845"},{"self" +:"https://example.com/rest/api/2/customFieldOption/12846","value":"Fr +iday","id":"12846"},{"self":"https://example.com/rest/api/2/customFie +ldOption/12847","value":"Saturday","id":"12847"}],"customfield_22380" +:"2016-02-20","customfield_22181":"Jira TEST-10756 Automated Testing" +,"customfield_11179":"2015-12-11","updated":"2016-01-06T14:15:14.000- +0500","status":{"self":"https://example.com/rest/api/2/status/10034", +"description":"","iconUrl":"https://example.com/images/icons/statuses +/generic.png","name":"ProdStaged","id":"10034","statusCategory":{"sel +f":"https://example.com/rest/api/2/statuscategory/4","id":4,"key":"in +determinate","colorName":"yellow","name":"In Progress"}}}}]}
    use strict; use warnings; use Benchmark qw( cmpthese timethese ); our $VERSION = '1.00'; my $wanttime = $ARGV[1] || 5; use JSON qw( -support_by_pp -no_export ); # for JSON::PP::Boolean inhe +ritance use JSON::XS (); my $json = <>; my perl = JSON::XS::decode_json $json; print Dumper $perl{issues} #get Global symbol "%perl" requires explic +it package name print Dumper $perl{issues}[0]{fields} #get Global symbol "%perl" requi +res explicit package name print Dumper @$perl{issues} prints output as in original post in VAR1.

      Could the absence of the $ in my perl = ... be a part of your problem?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.
      #!perl use strict; use warnings; use JSON::XS (); use Data::Dump 'pp'; # pretty print my $perl = JSON::XS::decode_json get_json(); #pp $perl->{'issues'}[0]; dump structure my $f = $perl->{'issues'}[0]{'fields'}; my $k = $perl->{'issues'}[0]{'key'}; #customfield_22181 as 'Jira Testing' print $f->{'customfield_22181'}."\n"; #customfield_11150{'value'} as 'Yes' print $f->{'customfield_11150'}{'value'}."\n"; #status-{'name'} as 'ProdStaged' print $f->{'status'}{'name'}."\n"; #customfield_11780{'value'} as "'Sunday', 'Monday',...,'Saturday'" print join ",",map{ $_->{'value'}} @{$f->{'customfield_11780'}}; print "\n"; #key as 'TEST-10756' print $k."\n"; sub get_json { return '{"expand":"names,schema","startAt":0,"maxResults":50,"total" +:1,"issues":[{"expand":"operations,versionedRepresentations,editmeta, +changelog,transitions,renderedFields","id":"569825","self":"https://e +xample.com/rest/api/2/issue/569825","key":"TEST-10756","fields":{"sum +mary":" Automated Testing","issuetype":{"self":"https://example.com/r +est/api/2/issuetype/10100","id":"10100","description":"","iconUrl":"h +ttps://example.com/secure/viewavatar?size=xsmall&avatarId=26414&avata +rType=issuetype","name":"Auto Testing","subtask":false,"avatarId":264 +14},"customfield_11150":{"self":"https://example.com/rest/api/2/custo +mFieldOption/11347","value":"Yes","id":"11347"},"customfield_11780":[ +{"self":"https://example.com/rest/api/2/customFieldOption/12841","val +ue":"Sunday","id":"12841"},{"self":"https://example.com/rest/api/2/cu +stomFieldOption/12842","value":"Monday","id":"12842"},{"self":"https: +//example.com/rest/api/2/customFieldOption/12843","value":"Tuesday"," +id":"12843"},{"self":"https://example.com/rest/api/2/customFieldOptio +n/12844","value":"Wednesday","id":"12844"},{"self":"https://example.c +om/rest/api/2/customFieldOption/12845","value":"Thursday","id":"12845 +"},{"self":"https://example.com/rest/api/2/customFieldOption/12846"," +value":"Friday","id":"12846"},{"self":"https://example.com/rest/api/2 +/customFieldOption/12847","value":"Saturday","id":"12847"}],"customfi +eld_22380":"2016-02-20","customfield_22181":"Jira TEST-10756 Automate +d Testing","customfield_11179":"2015-12-11","updated":"2016-01-06T14: +15:14.000-0500","status":{"self":"https://example.com/rest/api/2/stat +us/10034","description":"","iconUrl":"https://example.com/images/icon +s/statuses/generic.png","name":"ProdStaged","id":"10034","statusCateg +ory":{"self":"https://example.com/rest/api/2/statuscategory/4","id":4 +,"key":"indeterminate","colorName":"yellow","name":"In Progress"}}}}] +}'; };
      poj
        Thanks a lot poj. Worked like a charm. Missed the '->' between $perl and {'issues'} and that made a big difference. Why $perl{'issues'}[0]{'fields'} will not work?