
foggy
Novice
Sep 4, 2012, 11:45 AM
Post #12 of 12
(1791 views)
|
|
Re: [foggy] convert JSON hash / array into variables for use in Perl
[In reply to]
|
Can't Post
|
|
Sorry I've not been able to get back. Tesco web site has been down and I've only now had a chance to test the code. Just re-cap: This is my code
#!/usr/bin/perl use warnings; use strict; use WebService::Tesco::API; my $tesco = WebService::Tesco::API->new( app_key => 'xxxxxx7709742AA45', developer_key => 'xxxxAwUk97yp0', debug => 1, ); my $result = $tesco->login({ email => 'xxxx@yahoo.co.uk', password => 'xxxx57', }); 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 @lines = split /^/, $code; # converts multiline string into an array of lines foreach my $line (@lines) { # travels through each line my $product_id = $1 if $line =~ /^"ProductId": "(\d+)"$/; # if lines containt with '^ProductId": "'following by more than # 0 digits, capture these digits into $product_id print $product_id, "\n"; } This returns the following: https://secure.techfortesco.com/groceryapi_b1/restservice.aspx?email=xxxx%40yahoo.co.uk&password=xxxxx57&developerkey=xxxxxx&applicationkey=xxxxxx&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": "aCIjKUiLcwLCzxYYPquMRt6ZzdG5jjJWlPsARXBNLLIn8pW3XX", "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=aCIjKUiLcwLCzxYYPquMRt6ZzdG5jjJWlPsARXBNLLIn8pW3XX&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/IDShot_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/Sites/Retail/Superstore/Online/Product/pos/save.png", "Price": 1, "PriceDescription": "£1.41 each", "ProductId": "254881114", "ProductType": "QuantityOnlyProduct", "UnitPrice": 1.41, "UnitType": "100g" } ] } Use of uninitialized value $product_id in print at tesco_play1.pl line 24, <> line 1. I would appreciate any help on what I am doing wrong. Best wishes. James.
|