Hi all, I am new to programming and Perl. I am using a an API to return JSON data/array/hash and from that I need to create a variable from part of the data in order to create my next URL. From a previous request for help I have been advised that I should use the JSON module. I have tried to follow an example taken from google, but clearly failing to grasp the program logic or syntax.

#!/usr/bin/perl use warnings; use strict; use WebService::Tesco::API; use JSON; # My code to login removed print "Scan Item...."; my $pn = <>; #Scan Bar Code chop ($pn); # +remove %OA from end of scan code my $code = $tesco->product_search({ searchtext => $pn, extendedinfo => + 'N' }); my $json = JSON->new; my $data = $json->decode($code); my $pid = $data->{Products}[0]{ProductId},"\n"; print $pid,"\n";

Returned is:

Useless use of a constant ( ) in void context at tesc_json1.pl line 22. https://secure.techfortesco.com/groceryapi_b1/restservice.aspx?email=x +xxxx%40yahoo.co.uk&password=xxxxx&developerkey=xxxxxx&applicationkey= +xxxxx&command=LOGIN at /usr/local/share/perl/5.14.2/WebService/Tesco/ +API.pm line 59. { "StatusCode": 0, "StatusInfo": "Command Processed OK", "BranchNumber": "5528", "CustomerId": "20846164", "CustomerName": "Mr Stubbs", "SessionKey": "5VApkMMTjO8LtFCy9VjcSrcZKxoewwHGj6XoUUGaH2fBWf73cU", "InAmendOrderMode": "N", "BasketID": "98911496", "ChosenDeliverySlotInfo": "No delivery slot is reserved.", "CustomerForename": "James" } Scan Item....5000175411118 http://www.techfortesco.com/groceryapi_b1/restservice.aspx?searchtext= +5000175411118&extendedinfo=N&sessionkey=5VApkMMTjO8LtFCy9VjcSrcZKxoew +wHGj6XoUUGaH2fBWf73cU&command=PRODUCTSEARCH at /usr/local/share/perl/ +5.14.2/WebService/Tesco/API.pm line 59, <> line 1. { "StatusCode": 0, "StatusInfo": "Command Processed OK", "PageNumber": 0, "TotalPageCount": 1, "TotalProductCount": 1, "PageProductCount": 1, "Products": [ { "BaseProductId": "50043662", "EANBarcode": "5000175411118", "CheaperAlternativeProductId": "", "HealthierAlternativeProductId": "", "ImagePath": "http://img.tesco.com/Groceries/pi/118/5000175411118/IDSh +ot_90x90.jpg", "MaximumPurchaseQuantity": 99, "Name": "Oxo 12 Chicken Stock Cubes 71G", "OfferPromotion": "Price Drop Was £1.13 Now £1.00 ", "OfferValidity": "valid from 30/7/2012 until 9/9/2012", "OfferLabelImagePath": "http://www.tesco.com/Groceries/UIAssets/I/Site +s/Retail/Superstore/Online/Product/pos/save.png", "Price": 1, "PriceDescription": "£1.41 each", "ProductId": "254881114", "ProductType": "QuantityOnlyProduct", "UnitPrice": 1.41, "UnitType": "100g" } ] } malformed JSON string, neither array, object, number, string or atom, +at character offset 0 (before "HASH(0x1610440)") at tesc_json1.pl lin +e 20, <> line 1.

Clearly I have two (2) errors, despite googling, I am unable figure out what it is I am doing wrong. I would appreciate it if someone could assist.

Thank you - James


In reply to Help with JSON Module by foggy3657

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.