Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Perl Monk, Perl Meditation

Send JSON data to Perl scalar

by perl.j (Pilgrim)
on Dec 21, 2012 at 15:20 UTC ( #1009922=perlquestion: print w/ replies, xml ) Need Help??
perl.j has asked for the wisdom of the Perl Monks concerning the following question:

I currently have the following code which captures the number of followers for a particular Twitter account, and prints it to a webpage:

<html> <head> <script src="// +in.js"></script> <script type="text/javascript"> $(document).ready(function() { var twitterusername = 'ESPN'; $.getJSON('' + twitterusername + '.json?c +allback=?', function(data) { $('#followers').html(data.followers_count); document.write(twitterusername, "# of Followers: ", data.follo +wers_count); }); }); </script> </head> <body> <div id='followers'></div> </body> </html>

How can I take the number of followers, and store it into a Perl variable?


Comment on Send JSON data to Perl scalar
Download Code
Re: Send JSON data to Perl scalar
by Corion (Pope) on Dec 21, 2012 at 15:24 UTC

    I would look at the HTTP requests that this page makes and replicate them using LWP::Simple and JSON::Any.

    An alternative approach would be to use a module or program that understands Javascript and acts like a browser, for example WWW::Scripter or WWW::Mechanize::Firefox, and then read the information out of the resulting page.

      What if I don't print it to a webpage yet. Is there any way that I can just send the data directly to a Perl script?


        Neither LWP::Simple nor JSON::Any "print to a web page", so I'm not sure why you think that "printing to a web page" comes into play with the approaches I suggested. Maybe you want to read the documentation of JSON::Any or JSON::XS?

Re: Send JSON data to Perl scalar
by tobyink (Abbot) on Dec 21, 2012 at 15:46 UTC

    I think what you're looking for is...

    use LWP::Simple qw(get); use JSON qw(from_json); my $username = 'ESPN'; my $data = from_json get "${username}.json"; print $username, " has ", $data->{followers_count}, " followers.\n";

      This is great! My only issue here is speed, since this is going to be run about once every minute or two.


        I don't see why that would be a problem. That script takes about 1.2 seconds to execute on my laptop (and this machine is pretty old and underpowered), so could comfortably execute 30 or 40 times per minute.

        Though probably if I wanted to retrieve this data more than once a minute, I'd probably just add a loop to the script so that you execute it once and it runs in the background, repeatedly retrieving the data.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009922]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2014-03-17 19:41 GMT
Find Nodes?
    Voting Booth?

    Have you used a cryptocurrency?

    Results (356 votes), past polls