Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Bad index while coercing array into hash

by kwaping (Priest)
on Jun 08, 2007 at 22:58 UTC ( #620109=note: print w/ replies, xml ) Need Help??


in reply to Bad index while coercing array into hash

Your $self->result->{Abcd} is actually an array of hashrefs. Try printing $self->result->{Abcd}[0]{Platform} instead.


---
It's all fine and dandy until someone has to look at the code.


Comment on Re: Bad index while coercing array into hash
Select or Download Code
Re^2: Bad index while coercing array into hash
by Anonymous Monk on Jun 11, 2007 at 00:03 UTC
    thanks kwaping, its working...
      Can someone please help me on this : The output of the custom field when run via api query: "customfield_11872": [ { "self": "https://localhost.com:8080/rest/api/2/version/35372", "id": "35372", "description": "Null version. May be deleted after other conforming versions are created", "name": "0.0.0.0", "archived": false, "released": false } the command that i used is shown below: my $decoded_json = decode_json($result); $jira_found_in_release = "$decoded_json->{'fields'}{'customfield_11872'}{'name'}; When the customfield name value is null, I do not get any error but when the name field contains value such as 0.0.0.0 , i get bad index while corecing array into hash... Can someone please tell me how to store the value into $jira_found_in_release variable.

        Hmm, well, you can't treat a string as a hash or an array as a hash or a hash as an array

        You can use eval to trap errors, ref to interrogate values to see what kind of reference they are, or Data::Diver which combines both, Re: Parsing SOAP::Lite results (with Data::Diver)

        use strict; use warnings; use JSON qw/ encode_json decode_json /; my $json = <<'__JSON__'; { "fields" : { "customfield_11872" : [ { "name" : "0.0.0.0", "self" : "https://localhost.com:8080/rest/api/2/version/35 +372", "released" : false, "id" : "35372", "archived" : false, "description" : "Null version. May be deleted after other +conforming versions are created" } ] } } __JSON__ my $decoded_json = decode_json( $json ); print $decoded_json->{'fields'}{'customfield_11872'}[0]{'name'}, "\n"; use Data::Diver qw/ Dive /; if( my $toyArrRef = Dive $decoded_json, qw/ fields customfield_11872 / + ){ for my $toy ( @{ $toyArrRef } ){ if( my $name = Dive $toy, qw/ name / ){ print "Name is $name\n"; } } } __END__ 0.0.0.0 Name is 0.0.0.0

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://620109]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (20)
As of 2015-07-01 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (19 votes), past polls