CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
convert JSON hash / array into variables for use in Perl

 



foggy
Novice

Aug 21, 2012, 11:34 AM

Post #1 of 12 (3000 views)
convert JSON hash / array into variables for use in Perl Can't Post

Hi All,

Real newbie, only started trying to learn Perl a week ago and would like some help.
I am using a module WebService::Tesco::API
I have started to do some programming and have got as far as getting the following returned.

Code
 
Scan Item:5000175411118
http://www.techfortesco.com/groceryapi_b1/restservice.aspx?searchtext=5000175411118&extendedinfo=N&sessionkey=zzOvcQxRKx23Etmrtuu2d243sBvJcWQJpZexuQwl8QGYRDwySv&command=PRODUCTSEARCH at /usr/local/share/perl/5.14.2/WebService/Tesco/API.pm line 59, <> line 1.
{
"StatusCode": 0,
"StatusInfo": "Processed and Logged 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"
}
]
}

What I need to do now is to convert the returned ProductId "254881114" into a variable that I can use to send another http request.

Can any one assist in helping with some code or putting me in the right direction for some inspiration/further reading?

Your assistance will be appreciated.

Best wishes.

James.


Laurent_R
Veteran / Moderator

Aug 21, 2012, 11:54 AM

Post #2 of 12 (2998 views)
Re: [foggy] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Hi,

I am not sure how the data is returned to you (is it a file, a string, whetever?), but assuming it is a multi-line string stored in $foo, you could use a regular expression, for example do something like this:


Code
@lines = split /^/, $foo; # 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
}


Now, $product_id should contain 254881114 with the input you gave.


foggy
Novice

Aug 21, 2012, 1:18 PM

Post #3 of 12 (2992 views)
Re: [Laurent_R] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Thank you.

The data is returned from the following code.

Code
 

#!/usr/bin/perl
use warnings;
use strict;
use WebService::Tesco::API;


my $tesco = WebService::Tesco::API->new( app_key => '123456789', developer_key => '123456789ABCDEF', debug => 1, );

my $result = $tesco->login({ email => 'SomeOne@yahoo.co.uk', password => 'Secret', });

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' });

@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;


I assumed that I substitute $foo with $code

However I am getting the following error

Code
 
Global symbol "@lines" requires explicit package name at tesco_play.pl line 27.
Global symbol "@lines" requires explicit package name at tesco_play.pl line 28.
Search pattern not terminated at tesco_play.pl line 30.


Line numbers may not be correct as I cleaned up some of the additional comments I had in the code.

Best wishes.

James


Laurent_R
Veteran / Moderator

Aug 21, 2012, 1:35 PM

Post #4 of 12 (2987 views)
Re: [foggy] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

True, I forgot something. Change the first line as follows:


Code
my @lines = split /^/, $foo; # 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
}


Hopefully, this should work this time.


foggy
Novice

Aug 21, 2012, 1:58 PM

Post #5 of 12 (2984 views)
Re: [Laurent_R] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Thank you.

Actually adding "my" to the beginning was the first thing I tried when I got the errors.

But still getting

Code
Search pattern not terminated at tesco_play.pl line 30.


Best wishes.

James.


Laurent_R
Veteran / Moderator

Aug 21, 2012, 11:20 PM

Post #6 of 12 (2975 views)
Re: [foggy] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Right. Another error in my suggestion: Change the relevant line as follows:

my $product_id = $1 if $line =~ /"^ProductId": "(\d+)$/";


(Missing slash at the end).


foggy
Novice

Aug 22, 2012, 11:20 AM

Post #7 of 12 (2953 views)
Re: [Laurent_R] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Hi,

Sorry for so much grief. I really so appreciate your time and assistance.
Just to be sure I include all my code again.

Code
 
#!/usr/bin/perl
use warnings;
use strict;
use WebService::Tesco::API;


my $tesco = WebService::Tesco::API->new( app_key => 'xxxxxxxx', developer_key => 'xxxxxxxxxx', debug => 1, );

my $result = $tesco->login({ email => 'anyone@yahoo.co.uk', password => 'secret', });

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;


I am still getting the following errors
String found where operator expected at tesco_play1.pl line 18, near "# if lines containt with '^ProductId""
(Might be a runaway multi-line "" string starting on line 17)
(Missing semicolon on previous line?)
syntax error at tesco_play1.pl line 18, near "# if lines containt with '^ProductId""
Can't find string terminator '"' anywhere before EOF at tesco_play1.pl line 18.

A couple of things I have noticed. I am using Nano and it appears to show syntax highlighting.
/"^ProductId": "(\d+)/$ most of this is coloured yellow with the exception of the first / (Showing Black)
$line (@lines) most of this coloured blue with the exception of the first bracket (Showing Black).

Best Wishes.

James.


FishMonger
Veteran / Moderator

Aug 22, 2012, 11:29 AM

Post #8 of 12 (2950 views)
Re: [foggy] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post


Quote
my $product_id = $1 if $line =~ /"^ProductId": "(\d+)/$";


Probably should have been:

Code
my $product_id = $1 if $line =~ /^"ProductId": "(\d+)"$/;



Laurent_R
Veteran / Moderator

Aug 22, 2012, 11:49 AM

Post #9 of 12 (2947 views)
Re: [FishMonger] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Yes, the "/" should definitely come after the "$".


Laurent_R
Veteran / Moderator

Aug 22, 2012, 12:02 PM

Post #10 of 12 (2946 views)
Re: [foggy] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Hi

This will not work correctly:


Code
foreach my $line (@lines) { # travels through each line    
my $product_id = $1 if $line =~ /"^ProductId": "(\d+)$/";
# ...
}
print $product_id;


#product_id is defined within the loop and is local to the loop, and it is no longer defined where you try to print it.

it should be either:


Code
my $product_id; 
foreach my $line (@lines) { # travels through each line
$product_id = $1 if $line =~ /"^ProductId": "(\d+)$/";
# ...
}
print $product_id;

(except that it does not really make sense because you will print only the last product ID), or, far more probably:


Code
foreach my $line (@lines) { # travels through each line    
my $product_id = $1 if $line =~ /"^ProductId": "(\d+)$/";
# ...
print $product_id, "\n";
}


which will print each product ID, one per line.


foggy
Novice

Aug 22, 2012, 1:04 PM

Post #11 of 12 (2942 views)
Re: [Laurent_R] convert JSON hash / array into variables for use in Perl [In reply to] Can't Post

Thank you both for your replies.

Unfortunately the website server appears to be down and I can't try your suggestions.

As soon as I am able - I will confirm the issue resolved.

Laurent_R. Thank you. Your comments make sense and I have learnt some thing.

Best wishes.

James.


foggy
Novice

Sep 4, 2012, 11:45 AM

Post #12 of 12 (2455 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


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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives