Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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?


Replies are listed 'Best First'.
Re: Send JSON data to Perl scalar
by Corion (Patriarch) 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 (Canon) 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";
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      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.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Domain Nodelet?
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 pondering the Monastery: (1)
As of 2023-06-04 21:48 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (22 votes). Check out past polls.