API

JSON Basics

This post is a part of our How to begin with API Testing series. In this post, I will be covering JSON Basics.

What is JSON:

Like XML, JSON is also a communication format used for sharing data between multiple applications. JSON stands for JavaScript Object Notation. It is basically collection of key-value pair.

JSON can be used in following ways:

  • JSON Object: 

Below Object has 3 key-value pairs


    { "firstName" : "Vijay", "lastName" : "Gupta", "gender" : "Male" }

 

 

json_object

 

  • JSON Array

Below array has 3 JSON Objects with 3 key-value pairs each

[

{"name" : "Potato","stock" : "10", "unit" : "kg"},

{"name" : "Tomato","stock" : "32", "unit" : "kg"},

{"name" : "Onion","stock" : "27", "unit" : "kg"}

]

 
json_array

 

JSON Syntax Rules:

  • Data is represented in name/value pairs.
  • Name and Value are delimited / separated by colon (:)
  • Each Name / Value pair are separated by , (comma)
  • Curly braces hold objects
  • Square brackets hold arrays
  • Each JSON Object in array  are separated by ,(comma)

Nested JSON Objects:

JSON Objects can be nested also:

{

"id": "0001", "type": "donut", "name": "Cake",

"image":

{ "url": "images/0001.jpg", "width": 200, "height": 200},

"thumbnail":

{"url": "images/thumbnails/0001.jpg", "width": 32, "height": 32}

}

 

XML Vs JSON:

XML JSON
Origin 1998 2006
Ancestor SGML JavaScript
Verbose More Less
Arrays No Yes
Namespace Support Yes No
File Format .xml .json
Display Capabilities Yes No
Orientation Document Oriented Data Oriented
Advertisements

1 reply »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s