0

I am trying parsing JSON from a website. I am having problem in parsing JSON. I am getting following error:

malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "<html...") at D:\parser.pl line 45.

The URL is: https://coinmarketcap.com/coins

Any help would be appreciated. My code snippet is:

use strict;
use warnings;
use LWP::UserAgent;
use LWP::Simple;
use HTML::TreeBuilder;
use JSON;
use IO::Socket::SSL;
use Data::Dumper;

sub getJSON {
    my $url = $_[0] or die "URL missing!\n";
    
    my $ua = LWP::UserAgent->new(
    ssl_opts => {
        SSL_verify_mode => SSL_VERIFY_NONE(),
        verify_hostname => 0,
    }
);
    my $req = HTTP::Request->new( GET => $url );
$req->content_type('application/json');
my $res = $ua->request($req);

 die $res->status_line unless $res->is_success;

my $json = JSON->new();
my $json_text = $json->decode( $res->content );
}
2
  • 5
    The main problem with this code is that the URL you access does not actually return JSON. So the error message is fully correct - you are trying to interpret something as JSON which is not JSON. The fix is therefore to use the correct URL, whatever this is. Apart from that: setting a Content-Type header on a GET request does not make sense. This header describes the type of the content in the request body. But since this is a GET request there is no request body which also means that this none existing body has no type. Commented Mar 7, 2021 at 11:15
  • I found the solution myself. I scanned the HTML of website page with tree builder and extracted the tag containing json with tag ID. I got the whole json in one variable as a string. Commented Mar 7, 2021 at 13:03

1 Answer 1

1

I found the solution myself. I scanned the HTML of the website page with tree builder (Perl API available on CPAN) and extracted the tag containing json with its tag ID. I got the whole json in one variable as a string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.