OmarElgabry's Blog
Published in

OmarElgabry's Blog

JSON In a Nutshell

Storing and exchanging data especially between a server and web application is a major practice for most developers and even beginners. XML has been on the scene for several years, But there is an alternative solution called “JSON”.

Getting Started

1. What’s JSON?!

2. JSON Syntax

Object Declaration

{ 
"id": 4911,
"firstName":"John",
"lastName":"Walter",
"isStudent": true
};
  • An Object is a collection of name/value pairs
  • It starts with the left curly brackets ‘{‘ and ends with right curly brackets ‘}’
  • Each name in the Object is a string in a double quotation, and it’s followed by a colon ‘:’ then the value associated with that name.
  • name/value pairs are separated by comma ‘,’

Array Declaration

[
"Belgium", "Brazil", "Cameroon", "Canada", "Chile", "Denmark"
]
  • Arrays are ordered lists of values of the zero-based index.
  • It starts with the left bracket ‘[‘ and ends with right brackets ‘]’
  • Values are separated by comma ‘,’

3. JSON Values

  1. a string in double quotes
  2. a number
  3. boolean; true or false
  4. null
  5. an object
  6. an array

3. Putting All Together — Facebook API

1. JSON data

{
"data": [
{
"id": "X12",
"from": {
"name": "John Kennedy", "id": "1458633"
},
"message": "JSON is much easier and better than XML",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T22:27:44+0000",
"updated_time": "2010-08-02T22:27:44+0000"
},
{
"id": "X45",
"from": {
"name": "Anna Smith", "id": "12587521"
},
"message": "On my way to write my very first JSON snippet",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2010-08-02T25:27:44+0000",
"updated_time": "2010-08-02T25:27:44+0000"
}
]
}

2. Ajax Call

$.getJSON( "facebook.json", successFn);

3. Loop through JSON data

function successFn(jsonData,status, xhr){
// facebookPosts is an array, same as "data" in json file.
var facebookPosts = jsonData.data;
for (var i=0; i < facebookPosts.length; i++){ var id = facebookPosts[i].id;
var from = facebookPosts[i].from.name;
var msg = facebookPosts[i].message;
var type = facebookPosts[i].type;
var firstAction = facebookPosts[i].actions[0].name;

console.log(from+ " of ID: "+id+" wrote: "+msg+" as a "+type);
// John Kennedy of ID: X12 wrote: JSON is much easier and better than XML as a status
// Anna Smith of ID: X45 wrote: On my way to write my very first JSON snippet as a status
}

}

Download the code snippet, and share if you find it useful.

--

--

This is my freedom area. Don't underestimate it. The devil is in the detail.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store