vskatusa has asked for the wisdom of the Perl Monks concerning the following question:

Hi I want to read a https url and basically use decode_json; As an example i know i can read inline data (DATA) like so :
use JSON qw( decode_json ); my $json_array = decode_json ( do { local $/; <DATA> } ); __DATA__ Json text
How do I read the json from https url? Something like
use JSON qw( decode_json ); my $json_array = decode_json ( 'https://someurl?sss&output=json' );

Replies are listed 'Best First'.
Re: How to read https json?
by Corion (Patriarch) on Jan 09, 2018 at 19:38 UTC

    For retrieving the URL, see (for example) LWP::Simple:

    use LWP::Simple; ... my $content = get 'https://someurl?sss&output=json'; my $json_array = decode_json( $content ); ...
      Thank you... I figured it out. For the benefit of other who may lookup this thread
      my $url = 'https://xxxx.com/sd&output=json'; use LWP::Simple qw/get/; my $content = get $url; die "Couldn't get $url" unless defined $content; $content =~ s/\/\///; ## had to remove // in front #print Dumper $content; my $json_array = decode_json($content);
      the above code works!

        I would recommend that you change

        $content =~ s/\/\///; ## had to remove // in front
        into
        $content =~ s{\A//}{}; ## remove // at the beginning of the json cont +ent
        ... where I used the s{}{} notation to avoid leaning matchstick syndrome (thus avoid escaping the / as \/, ie, leaning matchsticks: see perldoc perlop Quote and Quote-like Operators). I also added the \A beginning-of-string anchor (see perlre, search for "Assertions"), because your regexp would have deleted the first //, whether it's at the beginning, or embedded in important data in your json. If there might be spaces before the //, then
        $content =~ s{\A\s*//}{}; ## remove // at the beginning of the json c +ontent

        Sidebar, comments are illegal in JSON so what you are handling is not JSON and neither is the "Json text" in your original __DATA__. decode_json was surely telling you so with malformed JSON string, neither tag, array, object, number, string or atom, at character offset 0 (before "Json text") or garbage after JSON object.

        If it were me, I'd report it to the owner of the application issuing it. Issuing, and accepting if you can help it, broken data is a worst practice.