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

Dear Monks,
My task is for a given request through URL to get results which is JSON encoded. Am I right that I need to do something like that:
$ua = LWP::UserAgent->new(); $body = $ua->get($url); $json = JSON->new->allow_nonref; $perl_scalar = $json->decode( $body->decoded_content );
I did not test it, but sometimes even if it works this may not be the right way to do. So could you give me a right solution for this standard problem, please.

Replies are listed 'Best First'.
Re: Parse JSON response
by kennethk (Abbot) on Jun 06, 2011 at 20:27 UTC
    The posted code is generally correct. In order to know details, it is necessary to know more about your source. For example, if the host does not specify a content encoding, $body->decoded_content will fail. If the source is strict JSON, you should not allow non-references. If this is done in an automated fashion, you should add a time out and error handling to your ua. This should all be straight-forward once you start developing in earnest.