Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

decoding a JSON object

by anautismobserver (Sexton)
on Nov 23, 2022 at 00:58 UTC ( #11148313=perlquestion: print w/replies, xml ) Need Help??

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

This is a dumb question that is taking me hours but which a monk would solve in seconds.

Below is code that returns a JSON object.

use strict; use warnings; use Mojo::UserAgent; my $ua_food; my $json_food; my $url_food; $url_food = " +D3tFWgPAscW0R0gBRivwqEdt0N182PXCGNQO"; $ua_food = Mojo::UserAgent->new; $json_food = $ua_food->get($url_food)->res->json;

Please tell me how to extract the values for fdcId and the various nutrient items.

Thank you.

Replies are listed 'Best First'.
Re: decoding a JSON object
by marto (Cardinal) on Nov 23, 2022 at 13:39 UTC

    The same as the last time you asked, and the time before that :P

    #!/usr/bin/perl use strict; use warnings; use Mojo::UserAgent; use feature 'say'; my $url_food = ' +c4bD3tFWgPAscW0R0gBRivwqEdt0N182PXCGNQO'; my $ua_food = Mojo::UserAgent->new; my $json_food = $ua_food->get($url_food)->res->json; say $json_food->{fdcId}; foreach my $nutrients( @{$json_food->{'foodNutrients'}} ){ say $nutrients->{'nutrient'}{'name'}; }

      You're right (and diligent). Sorry for wasting your time. I was tired and frustrated and not thinking clearly.

        It happens to us all, and these are the right circumstances to ask others for help. Don't worry about it :)

Re: decoding a JSON object
by 1nickt (Canon) on Nov 23, 2022 at 12:49 UTC


    It's a little confusing but $mojo->tx->res->json returns the de-serialized data from the JSON response.

    So for clarity you should rename your $json_food variable to, e.g., $data_food. You can then access elements of the hashref as you would normally, e.g. $data_food->{fdcId}.

    Hope this helps!

    The way forward always starts with a minimal test.
Re: decoding a JSON object
by kcott (Archbishop) on Nov 23, 2022 at 02:52 UTC

    G'day anautismobserver,

    Decode (to a Perl data structure) using JSON. If you also have JSON::XS installed, it will run faster. You can use JSON::XS directly, but some users who maybe can't compile XS code will not be able to run your script. [See Update below]

    Once you have the JSON data in a Perl format, getting the wanted values is pretty straightforward. See "perldsc - Perl Data Structures Cookbook" if you don't know how to do this.

    Update: I took "Below is code that returns a JSON object." on face value and, in line with the title, "decoding a JSON object", provided a generic response on how to achieve that. I think you can be forgiven for assuming a method called json() returns JSON; however, that's not the case — see ++1nickt's explanation.

    — Ken

      Sorry, but the first paragraph is not applicable here - Mojo has JSON decoding already built in, and $json_food will already contain decoded JSON due to the ->json call.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (1)
As of 2022-11-26 20:15 GMT
Find Nodes?
    Voting Booth?