Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Undefined value as an ARRAY reference, JSON data

by johnfl68 (Scribe)
on Apr 14, 2015 at 17:35 UTC ( [id://1123429]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I am reading data from JSON, from an external API source.

On some occasions, one of the Arrays in the JSON is not present, because there is no applicable data. This causes the Undefined value as an ARRAY reference error.

I've been looking around for a while for a way to get some working solution out of the error, so I know to just not process that data. But every post I have read makes the assumption that the data is flawed because the array I am looking for should always be there, which isn't the case.

It's like a catch 22, I need to read it to check and see if it is defined, but I can't read it because it is undefined.

Can anyone please point me in the right direction to handle this error? I am sure it is something simple that is just escaping me.

Thank you!

Replies are listed 'Best First'.
Re: Undefined value as an ARRAY reference, JSON data
by afoken (Chancellor) on Apr 14, 2015 at 18:06 UTC

    So, how does the JSON data look like, with and without the problematic array? Also show the relevant code.

    From your description, I guess something like this for the JSON:

    With the array:

    { "foo" : 42, "bar" : 4711, "the_array" : [ 1,2,3 ] }

    Without the array:

    { "foo" : 42, "bar" : 4711 }

    I assume that you use JSON::XS or something with a similar API, so after a call to decode_json(), you have a hash reference like this:

    $data={ foo => 42, bar => 4711, the_array => [ 1,2,3 ] }

    Or like this:

    $data={ foo => 42, bar => 4711 }

    If your code accesses the array as @{$data->{'the_array'}}, it will of course fail. Check with exists $data->{'the_array'} before accessing it.

    Maybe the JSON generator writes "the_array" : null in the problematic case. In that case $data->{'the_array'} will exist in Perl, but it will be undef. Test with defined $data->{'the_array'} before accessing the array.

    With recent perl versions, you can also "replace" a missing array with a default array using the defined-or operator: @{$data->{'the_array'}//['default','values','here']}.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thank you afoken, your exists $data->{'the_array'} was what I was looking for, and solved my problem.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-03-29 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found