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

Getting specific field values from JIRA rest/api/2/search?jql=filter=47080&fields=summary,issuetype,updated,status,customfield_11179, customfield_22380,customfield_11150,customfield_22181,customfield_11780 output using Perl module JSON::XS
my $perl = JSON::XS::decode_json $json; print Dumper @$perl{'issues'};
give output below:
$VAR1 = [ { 'fields' => { 'customfield_22181' => 'Jira Testing', 'customfield_11150' => { 'value' => 'Yes', 'self' => 'https://example.net/rest/api/2/customFieldOption/1 +1347', 'id' => '11347' }, 'status' => { 'statusCategory' => { 'colorName' => 'yellow', 'name' => 'In Progress', 'self' => 'https://example.net/rest/api/2/statuscategory/4', 'id' => 4, 'key' => 'indeterminate' }, 'name' => 'ProdStaged', 'iconUrl' => 'https://example.net/images/icons/statuses/generi +c.png', 'self' => 'https://example.net/rest/api/2/status/10034', 'id' => '10034', 'description' => '' }, 'customfield_22380' => '2016-02-20', 'customfield_11179' => '2015-12-11', 'customfield_11780' => [ { 'value' => 'Sunday', 'self' => 'https://example.net/rest/api/2/customFieldOption +/12841', 'id' => '12841' }, { 'value' => 'Monday', 'self' => 'https://example.net/rest/api/2/customFieldOption +/12842', 'id' => '12842' }, { 'value' => 'Tuesday', 'self' => 'https://example.net/rest/api/2/customFieldOption +/12843', 'id' => '12843' }, { 'value' => 'Wednesday', 'self' => 'https://example.net/rest/api/2/customFieldOption +/12844', 'id' => '12844' }, { 'value' => 'Thursday', 'self' => 'https://example.net/rest/api/2/customFieldOption +/12845', 'id' => '12845' }, { 'value' => 'Friday', 'self' => 'https://example.net/rest/api/2/customFieldOption +/12846', 'id' => '12846' }, { 'value' => 'Saturday', 'self' => 'https://example.net/rest/api/2/customFieldOption +/12847', 'id' => '12847' } ], 'issuetype' => { 'name' => 'Auto Testing', 'iconUrl' => 'https://example.net/secure/viewavatar?size=xsmall +&avatarId=26414&avatarType=issuetype', 'self' => 'https://example.net/rest/api/2/issuetype/10100', 'id' => '10100', 'subtask' => bless( do{\(my $o = 0)}, 'JSON::PP::Boolean' ), 'description' => '', 'avatarId' => 26414 }, 'summary' => ' JIRA testing Summary', 'updated' => '2016-01-06T14:15:14.000-0500' }, 'expand' => 'operations,versionedRepresentations,editmeta,changelog, +transitions,renderedFields', 'self' => 'https://example.net/rest/api/2/issue/569825', 'id' => '569825', 'key' => 'TEST-10756' } ];
But I want to get the values for:
customfield_22181 as 'Jira Testing' customfield_11150{'value'} as 'Yes' status-{'name'} as 'ProdStaged' customfield_11780{'value'} as "'Sunday', 'Monday',...,'Saturday'"u key as 'TEST-10756' + other customfields

Please advise.
Thanks

Code tags added by GrandFather

  • Comment on 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: Getting specific field values from JIRA rest/api output using JSON::XS and array of hashes
by 1nickt (Canon) on Jan 20, 2016 at 05:21 UTC

    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.
      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
Re: Getting specific field values from JIRA rest/api output using JSON::XS and array of hashes
by NetWallah (Canon) on Jan 20, 2016 at 05:21 UTC
    You should be able to get:
    my $fields = $perl->{issues}[0]{fields}; # Now you can get my $customfield_22181 = $fields->{customfield_22181}; # Should get + you the desired 'Jira Testing' my $customfield_11150_val = $fields->{customfield_11150}{value}; # Sho +uld be 'Yes' my @weekdays = map {$_->{value}} @{ $fields->{customfield +_11780} };
    Note : customfield_11780 should be an array of hashes .. the dump formatting does not make this clear.

    You use <pre> tags where you should have used <code> tags.

    Untested.
    Update:Changed $perl{ to $perl->{

            "I can cast out either one of your demons, but not both of them." -- the XORcist

Re: Getting specific field values from JIRA rest/api output using JSON::XS and array of hashes
by RonW (Parson) on Jan 20, 2016 at 23:33 UTC