jQuery Getjson Function Example

Let's have a closer look at the jQuery .getjson() function.

Demo Code Download


The $.getJSON() function is an AJAX shorthand method for accessing JSON data files. JavaScript Object Notation (JSON) provides a great way to store and transfer data independantly and it works great with JavaScript (because it's essentially the same notation). There are a few reasons why you might choose JSON over XML.

For this demo you can view the JSON file. We can also use functions like $.parseJSON() and JSON.parse() from ECMAScript 5 that simplifies JSON parsing. If you're interested in JSON Parsing and Tree Recursion see JSON Tree Viewer which is very interesting. Lets have a look at an example of the getjson jQuery function in action. Further Reading »


jQuery Function Demo - jQuery4u.com

Demo: Using $.getJSON() to populate options in a drop down list.

Run Demo View Code


The download package includes all the inline HTML, CSS, JavaScript, jQuery and images required for the .getjson() function demo so you can try it yourself and change as you please.

The full code listing for all jQuery functions have been now added to GitHub, you can get them there, we are making them work with the latest versions of jQuery!


<!-- Copyright 2011 jQuery4u.com -->
<!DOCTYPE html>
<title>jQuery Function Demo - jQuery4u.com</title>
    <script src="http://www.jquery4u.com/function-demos/js/jquery-1.6.4.min.js"></script>
    <script src="http://www.jquery4u.com/scripts/function-demos-script.js"></script>

    <script type="text/javascript">

    var JQFUNCS =
            /* ------------------------------ getJSON Demo ------------------------------ */
                run: function(id)
                    $.getJSON('/function-demos/data/colors.json', function(data)
                        var options = $('#'+id+' select');
                        $('#'+id+' select').append('<option value="">Select Colour</select>');
                        $.each(data.colors[0], function(i,v) {
                            options.append($("<option />").val(v).text(i));

                    $('#'+id+' select').live('change', function(e)

                reset: function(id)
                    $('#'+id+' select').html('');



<p class="example">Demo: Using $.getJSON() to populate options in a drop down list.</p>
<p><a href="#" id="getjson-demobtn" class="demobtn btn actionr">Run Demo</a> <a href="#" id="getjson-codebtn" class="codebtn btn actionr">View Code</a> <a href="#" id="getjson-resetbtn" class="resetbtn btn actionr" style="display: none;">Reset</a></p>
<div class="demoarea" id="getjson"><form style="width:200px;"><select></select></form></div>

<!-- Copyright 2011 jQuery4u.com -->