
In this post I want to illustrate how to use jQuery getJSON() method to load JSON data using an HTTP GET request. I prepared two examples, easy to understand if you are a jQuery beginner. The first example is a simple Twitter search and the second one is a simple Flickr search.
For a printable reference guide to the jQuery API I suggest you to download my jQuery Visual Cheat Sheet or take a look at the official jQuery documentation.
Using getJSON() to implement a Twitter Search
In this first example I used getJSON() to implement a simple Twitter Search that returns five results for page. Try to add something into input field below (for example, apple) and press the Search button to display search results:
This kind of search is really easy to implement just using some lines of JavaScript code. Add a link to jQuery into the <head> tag of the HTML page:
<script src="jquery.js" type="text/javascript"></script>In the <body> tag add this code:
<input type="text" id="twitterQuery" /><button button id="twitterButton">Search</button>
<div id="results"></div><div id="results"> is the <div> that will contain the search results.

Then add this JavaScript code into the <head> tag, after the link to jQuery:
$(document).ready(function(){
var url="http://search.twitter.com/search.json?rpp=5&callback=?&q=";
var query;
$("#twitterButton").click(function(){
$("#results").html('');
query=$("#twitterQuery").val();
$.getJSON(url+query,function(json){
$.each(json.results,function(i,tweet){
$("#results").append('<div class="tweet">
<img class="imgTweet" width="48" height="48"
src="'+tweet.profile_image_url+'"/>'+tweet.text+'
<strong>From:</strong>
<a href="http://twitter.com'+tweet.from_user+'">@'+tweet.from_user+'</a>
</div>');
});
});
});
});The previous code is really simple to understand. The url var contains the string http://search.twitter.com/search.json?rpp=5&callback=?&q= for the Twitter API call. rpp is the number of tweets to return per page (it’s optional). For a full reference guide to the Twitter API take a look at the official documentation. $("#results").append()> appends search results into the layer with id results.
Using getJSON() to implement a Flickr Search
This second example is similar to the previous one. I implemented a simple Flickr search using getJSON(). Try to add something into input field below (for example, wave) and press the Search button to display search results:
This kind of search is really simple to implement. Add a link to jQuery into the <head> tag of the HTML page:
<script src="jquery.js" type="text/javascript"></script>In the <body> tag add this code:
<input type="text" id="flickrQuery" /><button>Search</button>
<div id="results"></div>Then add this JavaScript code into the <head> tag, after the link to jQuery:
$(document).ready(function(){
var url="http://api.flickr.com/services/feeds/photos_public.gne?
&tagmode=any&format=json&jsoncallback=?&tags=";
var query;
$("#flickrButton").click(function(){
$("#results").html('');
query=$("#flickrQuery").val();
$.getJSON(url+query,
function(data){
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#results");
if ( i == 3 ) return false;
});
});
});
});The code above is really similar to the code you seen in the previous example. The url var contains the string http://api.flickr.com/services/feeds/photos_public.gne?&tagmode=any&format=json&jsoncallback=?&tags= for the Flickr API calls. $("#results").append() appends search results into the layer with id results. The if statement if (i==3) return false; limits the search results to 4. You can change that number as you prefer.
That’s all! Leave your comment if you have suggestions to improve the code.
by
Antonio Lupetti is an italian engineer, pro blogger, Mac user, founder of woorkup.com. He lives in Rome, Italy. Follow Antonio on 

January 6, 2010 at 11:50 am
Hi Antonio,
Two very nice, simple examples, but shouldn’t the actions take place on submission of the form, rather than click of the button? That approach makes things a lot easier for those of us who are heavy keyboard users.
- Bobby
January 15, 2010 at 6:09 am
Let me correct my own post: it appears that jQuery’s click() function DOES fire on a form submit, even if that submit occurred as a result of, for example, pressing the ENTER button. IMO, this is ridiculous and completely non-intuitive, but your code, Antonio, is beyond reproach in this regard :-)
January 6, 2010 at 2:10 pm
As a note, for this specific task, you could also use something like jTweetr – very useful little plugin.
http://plugins.jquery.com/project/jtweetr
January 6, 2010 at 2:45 pm
Thanks for the link Joseph.
January 6, 2010 at 4:20 pm
Wouldn’t this be just as easily implemented using jQuery’s .submit() ?
So instead of having:
$("#twitterButton").click(function(){you’d have:
$("#twitterButton").submit(function(){and of course change from a
<button>to a normal<input type="submit">Wouldn’t that work just as well? (I haven’t tested it…)
January 7, 2010 at 12:15 am
I didn’t think that is so easy ;]
January 7, 2010 at 6:07 am
Check out : http://www.chrispie.com/labs/twitpic-visualisation-619/
The twitpic realtime visualization (v1) gives you a snapshot of the images which are posted right now on twitter. For now the visualization only shows images of twitpic, in a later version i want to support more imagehoster.
January 8, 2010 at 8:45 am
I do not get it!
Where exactly do you place the javascript code ( $(document).ready(function(){
… etc. ) . Adding it to the Head section of the html page after the ref. to jquery give me an error (the script is showing when i view the page). Thanks.
January 8, 2010 at 10:14 am
OK fixed. But no result from the query – looks like javascriopt function is not launched. How to do it when the Head tag (on header.php page where i did put the javascript function) is on different php page then the input button search form (a sidebar.php page)?
January 8, 2010 at 3:43 pm
I did this exactly same thing today, but using Delicious API. I tend to always use $.ajax instead of $.get/post/load/getJSON, is there any penalty to do things this way?
January 10, 2010 at 10:42 am
Thanks for this Antonio – I’m new to JSON. Would it be possible to use the same technique to build a local search facility? If so, any chance of a tutorial?
January 13, 2010 at 8:02 pm
I’ve just added a Flickr photostream to my blog using jQuery. Great write up!