getJSON to read and display Json string data

Using Jquery we will develop code to handle Json data. For this we will use getJSON function and then using each looping we will display data.

We will start with simple Json object with a set of name value pairs. We have three examples here and each button is connected to one click event. To display the return data we have div element.

The Json data or the Json string is kept inside a .php file and for this example we have kept the string inside json1.php file.
Here is the string in Json format. You can keep in any file you want but output should match Json format.

{"id":"2","name":"Max Ruin","class1":"Three5","mark":"85"}

In the above example 'Show Data' button is connected to click event inside Jquery function block. WE read the Json string stored inside json1.php file.

$.getJSON("json1.php", function(return_data){

In above code we are reading the json string and storing them in return_data variable. Then we are using one each looping to display all name value pairs.
Here is the complete code.

<script >
$.getJSON("json1.php", function(return_data){
$.each(return_data, function(key, value){
$("#d1").append(key + " => " + value + "<br>");
<input type="button" id="b1" value="Show Data">
<div id='d1' style="background-color: #f1f1f1;"></div>

Returning array of data using Json

We will have several records returned by database and we have to use array of data to create Json string for data transfer. Here is one such string.

{"id":"1","name":"John Deo","class":"Four5",

{"id":"2","name":"Max Ruin","class":"Three5",


{"id":"4","name":"Krish Star","class":"Four5",

Now we have to modify the above code to display data by looping through each record.

Here is the source code of above demo

<script >
$.getJSON("json2.php", function(return_data2){
$.each(, function(key2, value2){
$("#d2").append(key_inside2 + "=>" + value_inside2 + "<br>");
<input type="button" id="b2" value="Show Data Multi Line">

<div id='d2' style="background-color: #f1f1f1;"></div>

Using Object and array of data as Json String

Now we have both array and some object data as Json string Here is the string of data stored in json3.php file.

The source code is here

<script >
$.getJSON("json3.php", function(return_data3){
$.each(, function(key3, value3){
$.each(value3, function(return_key3, return_value3){
$("#d3").append(return_key3 + "=>" + return_value3 + "<br>");
$.each(return_data3.value, function(return_key4, return_value4){
$("#d3").append(return_key4 + "=>" + return_value4 + "<br>");


Demo with all three types using getJSON

Script Download

The use the above demo you can download the zip file. Inside the main file is getJSON-demo.php file. The Json data string are kept inside json1.php, json2.php and json3.php file. Google SDN is used to link Json library so ensure that you are connected to internet while testing the script or you can download and use the Json library.

Download Zip file for getJson

Your Rating


Post Comment This is for short comments only. Use the forum for more discussions.

Most Popular JQuery Scripts


Two dependant list boxes


Calendar with Date Selection


Data change by Slider


Show & Hide element
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 All rights reserved worldwide Privacy Policy Disclaimer