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:
Help with JSON Error - Malformed JSON

 



foggy
Novice

Sep 7, 2012, 12:55 PM

Post #1 of 5 (1708 views)
Help with JSON Error - Malformed JSON Can't Post

Hi, I am having a problem with an error that is driving me crazy.

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


\\ Login code 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";

This returns:

Code
{ 
"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"
}
]
}
malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "HASH(0xa0b4b8)") at tesc_json1.pl line 20, <> line 1.

However this works:

Code
#!/usr/bin/perl 
use warnings;
use strict;
use JSON;

my $j = JSON->new;
my $d = $j->decode(<<'END');
{
"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"
}
]
}
END

# pp($d);

my $pid;

$pid = $d->{Products}[0]{ProductId}."\n";


print 'This is my ProductId: ';
print $pid,"\n";



foggy
Novice

Sep 8, 2012, 3:47 AM

Post #2 of 5 (1694 views)
Re: [foggy] Help with JSON Error - Malformed JSON [In reply to] Can't Post

I've got a little further. Using Data::Dumper. I get the following. But I don't understand what to do with it.

Code
 
$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/Groceries/pi/118/5000175411118/IDShot_90x90.jpg',
'Price' => 1,
'BaseProductId' => '50043662',
'Name' => 'Oxo 12 Chicken Stock Cubes 71G',
'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'
}
],
'TotalProductCount' => 1,
'PageNumber' => 0,
'TotalPageCount' => 1,
'StatusInfo' => 'Command Processed OK',
'PageProductCount' => 1
};



wickedxter
User

Sep 8, 2012, 6:08 AM

Post #3 of 5 (1685 views)
Re: [foggy] Help with JSON Error - Malformed JSON [In reply to] Can't Post

after looking at the cpan site for the json module the oop interface is useing

Code
 $json =JSON->new->allow_nonref; 
#then decode
$json->decode($data);



Laurent_R
Veteran / Moderator

Sep 8, 2012, 7:30 AM

Post #4 of 5 (1681 views)
Re: [foggy] Help with JSON Error - Malformed JSON [In reply to] Can't Post

If you try to print $$VARS1{'StatusCode'} or $VAR1->{'StatusCode'}, you should get the value, i.e. 0. Similarly, you could use $VAR1->{'Products'}[0]{'UnitType'} to get "100g".

But that is certainly not the right way to do it. The module you are using certainly provides methods for accessing your data. You should most probably use these methods.


foggy
Novice

Sep 8, 2012, 8:17 AM

Post #5 of 5 (1680 views)
Re: [Laurent_R] Help with JSON Error - Malformed JSON [In reply to] Can't Post

Thank you.

I think I was getting confused with what exactly type of data is being returned. I thought it was JSON, however I think the module is doing all the work and converting it to a Perl Hash and just doing


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


my $pid = $code->{Products}[0]{ProductId}."\n";


Is all I really need to do and just leave out the JSON decode.

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