in reply to Re^5: Help with JSON Module
in thread Help with JSON Module

Thank you. (How many times do I have to make that mistake.) Now I get

$VAR1 = { 'StatusCode' => 0, 'Products' => [ { 'UnitType' => '100g', 'HealthierAlternativeProductId' => '', 'OfferPromotion' => "Price Drop Was \x{a3} +1.13 Now \x{a3}1.00 ", 'ProductId' => '254881114', 'EANBarcode' => '5000175411118', 'MaximumPurchaseQuantity' => 99, 'CheaperAlternativeProductId' => '', 'ProductType' => 'QuantityOnlyProduct', 'PriceDescription' => "\x{a3}1.41 each", 'UnitPrice' => '1.41', 'ImagePath' => 'http://img.tesco.com/Groce +ries/pi/118/5000175411118/IDShot_90x90.jpg', 'Price' => 1, 'BaseProductId' => '50043662', 'Name' => 'Oxo 12 Chicken Stock Cubes 71G' +, 'OfferValidity' => 'valid from 30/7/2012 u +ntil 9/9/2012', 'OfferLabelImagePath' => 'http://www.tesco +.com/Groceries/UIAssets/I/Sites/Retail/Superstore/Online/Product/pos/ +save.png' } ], 'TotalProductCount' => 1, 'PageNumber' => 0, 'TotalPageCount' => 1, 'StatusInfo' => 'Command Processed OK', 'PageProductCount' => 1 };

Can you help with how to go on with the JSON decode statement?

my $code = $tesco->product_search({ searchtext => $pn, extendedinfo => + 'N' });

I am looking to create a variable that holds the value of "254881114" in this instance from ProductId.

Best wishes - James.

Replies are listed 'Best First'.
Re^7: Help with JSON Module
by Corion (Patriarch) on Sep 08, 2012 at 09:33 UTC

      Do I presume that $VAR1 was created by the Dumper module and can't be used in my program?

        Data::Dumper just outputs a string representation of your data. As you pass in an unnamed reference, Data::Dumper makes up a name. That name is $VAR1.

        You will have to learn about references, and start with the reference you also pass to Data::Dumper, instead of $VAR1.

Re^7: Help with JSON Module
by kcott (Archbishop) on Sep 08, 2012 at 11:38 UTC
    "Can you help with how to go on with the JSON decode statement?"

    I'm rather at a loss as to why you want to use JSON and what you want to decode.

    You've just demonstated that $tesco->product_search(...) returns Perl data (as a hashref) not JSON data.

    In your OP, you showed that you knew how to navigate through the data structure to get to the value you wanted:

    $data->{Products}[0]{ProductId}

    It seems to me that all you need is:

    my $data = $tesco->product_search(...); my $prod_id = $data->{Products}[0]{ProductId};

    -- Ken

      Probably because I am so confused / an idiot. I got the impression reading the Tesco API that data was returned as JSON and I don't know the difference between Hashref and JSON.

      Extract from the API - A simpler HTTP-based interface using elements of the Representation State Transfer (REST) methodology. This interface uses commands and parameters embedded in HTTP URL requests, known as query-strings. The interface responds with data in a format called JavaScript Object Notation (JSON).

      However, I guess perhaps the Webservice::Tesco::API module is taking care of all that and converting it to a Perl Hashref. I was trying to learn to do things the right way rather than what I mistakenly thought might be the wrong way.

      Just remains to say thank you, to you and the others that have taken the time to help me and point me in the right direction. I have learnt something along the way.

      Best wishes - James. (I'm sure I'll be back with my next issue).

        Don't beat yourself up too much. We all started knowing nothing.

        You've learned from this experience. Learn more about Perl references by following the links Corion provided. Learn more about JSON here: Introducing JSON.

        -- Ken