Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: How to read https json?

by vskatusa (Acolyte)
on Jan 09, 2018 at 20:02 UTC ( [id://1207011]=note: print w/replies, xml ) Need Help??


in reply to Re: How to read https json?
in thread How to read https json?

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!

Replies are listed 'Best First'.
Re^3: How to read https json?
by pryrt (Abbot) on Jan 09, 2018 at 20:24 UTC

    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

Re^3: How to read https json?
by Your Mother (Archbishop) on Jan 10, 2018 at 04:46 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-26 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found