Retrieving JSON Data Using jQuery

In this article, I am going to talk about JSON and jQuery. We will learn how to define simple and complicated JSON and how to retrieve the JSON data using jQuery.

What is JSON?

JSON refers to JavaScript Object Notation. We use JSON to transfer the data from server to client and client to server.

How to define JSON

JSON is always defined as key-value pair.

Eg

  1. var jsonObject = {  
  2.     "Key""value",  
  3.     "Key2""value2"  
  4. };  

In the above example, I have defined a JSON object that is having a unique key and its corresponding value.

  1. var JsonObject = {  
  2.     "facebookId""abc",  
  3.     "facebookPassword""123"  
  4. };  

In the above example, we can see that "facebookId" and "facebookPassword" are the keys and "abc" and "123" are their corresponding values.

How to retrieve JSON data using jQuery.

We can find the JSON object value using its key. Let us see how to retrieve JSON values.

First, we use object name and then put dot(.) followed by the key name.
  1. var facebookIdvalue = jsonObject.facebookId;  
  2. var facebookPasswordvalue = jsonObject.facebookPassword;  

Now, it's time to work with complicated JSON objects.

Level1

  1. var data1={"facebookId":"abc","facebookPassword":"123"}  ;  

Retrieving the data from JSON Object.

  1. var facebookPasswordvalue = jsonObject.facebookPassword;  

Level2

  1. var data2 = [{  
  2.     "Id""abc",  
  3.     "Password""123"  
  4. }, {  
  5.     "Id""xyz",  
  6.     "Password""1258"  
  7. }];  

Retrieving a single value

  1. var firstId = data2[0].id;  
  2. var secondid = data2[1].id;  
  3. var firstPassword = data2[0].Password;  
  4. var secondPassword = data2[1].Password;  

Retrieving all the values using Each loop in jQuery.

  1. $.each(data2, function (index, value) {  
  2.                     Console.log(value.Id);  
  3.                     Console.log(value.Password);  
  4.                 }); 

Level 3

  1. var userInfo= {  
  2.                   "shipping_address": {  
  3.                       "street_address""1600 Pennsylvania Avenue NW",  
  4.                       "city""Washington",  
  5.                       "state""DC"  
  6.                   }  
  7.               }  

Retrieving the data

  1. (firstkey= userInfo).(secondkey= shipping_address).(value= city)  
  2. var shippingAddCity= userInfo. shipping_address. city;  

Output  - “Washington”.

Level 4

  1. var comp = {  
  2.     "facebook": [{  
  3.         "userID""989",  
  4.         "Password""123"  
  5.     }, {  
  6.         "userID""nothing",  
  7.         "Password""NoPass"  
  8.     }, {  
  9.         "userID""nothing",  
  10.         "Password""NoPass"  
  11.     }, {  
  12.         "userID""nothing",  
  13.         "Password""NoPass"  
  14.     }]  
  15. };  

 

Retrieving the data
  1. var userdata= comp. facebook[0]. userID;  
  2. Output userdata=”989”  
Next Recommended Reading Return Data In JSON Format From Web API