JSON Interview Question


In this tutorial, We will explain some important JSON interview questions and answers.

1. What is JSON?

The below following points defines the JSON.

  • Douglas Crockford originally specified the JSON format in the early 2000s. He and Chip Morningstar sent the first JSON message in April 2001.
  • JSON stands for JavaScript Object Notation is a standard text-based format for representing structured data based on JavaScript object syntax .
  • JSON is the leading data interchange format for web applications.
  • JSON filenames use the extension .json

JSON is based on two types of structures:

  • A group of name/value combinations. This is implemented as an object, record, struct, dictionary, hash table, keyed list, or associative array in a variety of languages.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

These are universal data structures. They are essentially supported by all contemporary or modern programming languages. It makes sense for a data format to be built on these structures and to be convertible with computer languages.

2. Why use JSON?

When you start use JSON, Most developers have the question in mind why should we use JSON data format when we have other options like YAML, XML, etc. Before JSON, the communication between client and server was inefficient, as the server was overburdened with a heavy workload.

For instance, your web browser was only supposed to display things like outcomes of a web page search or any query result without any backend technicalities. The server was responsible for the remaining duties, which included handling and forwarding requests. Therefore, a simple, text-based structure like the JSON format was required to address all of these issues.

The points listed below make JSON more useable when compared to other formats.

  • JSON is a lightweight data-interchange format - JSON is more lightweight than XML because it mainly contains the data that needs to be transferred, rather than a lot of markup used to define the structure of the data.
  • JSON is used to send data between computers - JSON data can easily be sent between computers, and used by any programming language.
  • JSON is language independent can be combined with C++, Java, Python and many other languages.
  • JSON supports array, object, string, number and values.

3. What are the advantages of JSON?

The below following points defines JSON advantages.

  • JSON stores all the data in an array so data transfer makes easier. That’s why JSON is the best for sharing data of any size even audio, video, etc.
  • It has fairly simple syntax. It executes and responds more quickly because of the short size and light weight of its syntax.
  • Easy parsing makes it a convenient choice for storing and transmitting data.
  • JSON has a wide range for the browser support compatibility with the operating systems, it doesn’t require much effort to make it all browser compatible.
  • JSON is supported by many different programming languages and technologies, including JavaScript, Python, Ruby, and many others.

4. What are the disadvantages of JSON?

The below following points defines JSON Disadvantages.

  • The biggest drawback of JSON is the lack of error handling; if there is even a small problem in the JSON script, you will not receive structured data.
  • When used with some unapproved browsers, JSON can be rather harmful.
  • We can only use a small number of supported tools when developing JSON.

5. How JSON Works?

When you enter your username and password into a form on a web page, you are interacting with an object with two fields as username and password. As an example, consider the login page in Figure.

JSON for a Login Page

{
username: "Rohatash Kumar",
password: "Test@123" }

Everything inside of the brackets ( {...} ) belongs to the same object. An object, in this case, refers in the most general sense to a single thing. Inside the braces are the properties that belong to the thing. Each property has two parts: a name and a value, separated by a colon. These are known as the keys and values. In this JSON, username is a key and Rohatash Kumar is a value.

6. What are the difference between the JSON & XML

SNo.  JSON    XML 
1 JSON stands for javascript object notation. XML stands for an extensible markup language.
2 The extension of json file is .json. The extension of xml file is .xml.
Less code is created than XML More code require than JSON
4 The data types supported by JSON are strings, numbers, Booleans, null, array. XML data is in a string format.
5 JSON has no tags. XML data is represented in tags with start tag and end tag.
6 JSON can use arrays to represent the data. XML does not contain the concept of arrays.
7 It is less secure than XML. It is more secure than JSON.
8 JSON is data-oriented. XML is document-oriented.
9 The internet media type is application/json. The internet media type is application/xml or text/xml.
10 It is a language-independent data-interchange format. It is an independent data format.

7. What are the data types supported by JSON?

In JSON, data is represented as key/value pairs. The keys are strings(Text), and the values can be one of the following types.

  1. String
  2. Number
  3. Object
  4. Array
  5. Boolean
  6. Null

8. What is the key-value pair in JSON?

JSON uses key-value pairs to express an object's property. In the above example, we tried to represent a Person. This person has some properties like

  • First Name
  • Age
  • Address

Each of these properties have a value associated with it. For instance, the value of First Name is Rohatash. Age also has a value of 35. The rules for writing a Key-Value in JSON are as follows.

  • Key-value pairs are separated by a : (colon)
  • Key is always present in Double Quotes " "
  • Values must be valid JSON data types: string, number, another JSON object, array, boolean or null.

From the above example, a Key-Value pair is FirstName.

"FirstName" : "Rohatash" 

In the below example firstname, age, address are the keys and rohatash, 35, mathura defines the values.

{"FirstName" : "Rohatash","Age" : 35,"Address" : "Mathura"}

9. What is JSON Object

In JSON an object is represented by a collection of Key-Value pairs. This collection of Key-Value pairs are grouped using { } (opening and closing curly braces}. The rules for writing a Key-Value in JSON are as follows.

  • Key-Value pairs should be separated by a, (Comma)
  • Each Object should Start with an Opening {(Opening Curly Brace).

Example

In this example, Object start with { (Opening Curly Brace) and end with } (Ending Curly Brace).

{
   "FirstName" : "Rohatash",
   "Age" : 35,
   "Address" : "Mathura"
} 

10. What is Nested JSON Object

Objects can be nested inside other objects. Each nested object must have a unique access name.

Example

In this example, we have created a object qualification inside object.

{  
     "firstName": "Rohatash",       
     "age": 36,  
     "Qualification" : {  
         "Masterdegree": "MCA",  
         "Graduation": "BCA",         
     }  
}  

11. What is JSON Array

An array is a collection or list of data as defined in other language. An array in JSON is a group of values that are separated by commas. The rules for writing an Array in JSON are as follows.

  • An Array starts with an opening [(Bracket)
  • An Array ends with a closing ](Bracket)
  • Values in the Array are separated by , (Comma)s

Example

In this example, we have created a array qualification inside object. The following example show the simple JSON array:

{
"FirstName" : "Rohatash",
"Age" : "35",
"Address" : "Mathura"
"Qualification" : ["MCA", "BTech", "BCA"]
}

12. How can define comment in JSON

JSON is not a programming or scripting language but used by Programming and scripting languages like JavaScript, PHP, Python, C#, Java, etc. There is no natural way to comment on data in JSON. We can use the JSON key to build our own comment structure even though JSON does not by default support comments.

Douglas Crockford wrote:

"I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability, writes Douglas Crockford, who popularized the text-based data format."

The following example JSON stores the name and age values with the _comment key which is used for commenting.

{
   "name": "Rohatash", "age": 35,
    "_comment": "This is a comment",
}

Here _comment Key can be treated as comment.

13. What is the difference between JSON and JSONP?

  • JSON: JSON is a simple data format used for communication medium between different systems
  • JSONP: It is a methodology for using that format with cross domain ajax requests while not being affected by same origin policy issue.

14. What is JSON Parse?

JSON parsing is the process of converting a JSON object in text format to a Javascript object that can be used inside a program. Suppose you make a API project which return JSON Object. when You will recieve that JSON data from a web server, the data is always a string. Parse the data with JSON.parse(), and the data becomes a JavaScript object.

Example

Suppose we have a web API and it is uploaded on web server. So we received this text from a web server:

'{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}'

Use the JavaScript function JSON.parse() to convert text into a JavaScript object:

const parseobj = JSON.parse('{{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}}');

The below example you can use on HTML page

<html>
<body>

<h2>JavaScript Object from a JSON String</h2>

<p id="parsetestid"></p>

<script>
const jsontxt = '{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}'
const obj = JSON.parse(jsontxt);
document.getElementById("parsetestid").innerHTML = obj.name + ", " + obj.age + ", " + obj.Email;
</script>

</body>
</html>

15. What is JSON Stringify?

JSON stringify is the process of converting a javaScript object in JSON object that can be used inside a program. when You sending data to a web server, the data should be in string format. So you need to convert a JavaScript object into a string with JSON.stringify().

Example

Suppose we have a object in JavaScript as following:

const javascriptobject={name:"Rohatash", age:36, email:"test@gmail.com"};

Use the JavaScript function JSON.stringify() to convert JavaScript object into a string.

const myJSON = JSON.stringify(javascriptobject);

The below example you can use on HTML page

<html>
<body>

<h2>JSON String from a JavaScript Object </h2>

<p id="parsetestid"></p>

<script>
const javascriptobject= {name:"Rohatash", age:36, email:"test@gmail.com"};
const myJSON = JSON.stringify(javascriptobject);
document.getElementById("parsetestid").innerHTML = myJSON
</script>

</body>
</html>

16. How to use JSON data in JavaScript application?

The below example show how we can use JSON data in JavaScript Application.

Example

Suppose we have a web API and it is uploaded on web server. So we received this text from a web server:

'{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}'

Use the JavaScript function JSON.parse() to convert text into a JavaScript object:

const parseobj = JSON.parse('{{"name":"Rohatash", "age":36, "Email":"test@gmail.com"}}');

The below example you can use on HTML page

<html>
<body>
<h2>JavaScript Object from a JSON String</h2>
<p id="parsetestid"></p>
<script>
   const jsontxt = '{"name":"Rohatash", "age":36, "Email":"=test@gmail.com"}'
   const obj = JSON.parse(jsontxt);
   document.getElementById("parsetestid").innerHTML = obj.name + ", " + obj.age + ", " + obj.Email;
</script>
</body>
</html>

17. How to use JSON data in angular application?

The below example show how we can use JSON data in angular Application.

Example

Create an Angular application

The following command uses the Angular CLI to create a Angular application. The application name in the following example is jsoninangular.

ng new jsoninangular

Suppose we have a JSON as following:

[{
  "id": 1,
  "name": "Rohatash", 
  "email": "Roh@gmail.com"
},
{
  "id": 2,
  "name": "Gaurav",  
  "email": "Gau@gmail.com"
},
{
  "id": 3,
  "name": "Rahul",  
  "email": "rah@gmail.com"
},

{
  "id": 2,
  "name": "Ravindar",  
  "email": "rav@gmail.com"
},
{
  "id": 2,
  "name": "Ritesh",  
  "email": "rit@gmail.com@melissa.tv"
},
]

The below code use the above json on app.component.ts page.

import { Component } from '@angular/core';

interface employee {
  id: Number;
  name: String; 
  email: String;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  employeeJson=[{
  "id": 1,
  "name": "Rohatash", 
  "email": "Roh@gmail.com"
},
{
  "id": 2,
  "name": "Gaurav",  
  "email": "Gau@gmail.com"
},
{
  "id": 3,
  "name": "Rahul",  
  "email": "rah@gmail.com"
},

{
  "id": 2,
  "name": "Ravindar",  
  "email": "rav@gmail.com"
},
{
  "id": 2,
  "name": "Ritesh",  
  "email": "rit@gmail.com@melissa.tv"
},
]


  Users: employee[] = this.employeeJson;
  constructor(){
    console.log(this.Users);
  }
}

Now add the following Code on app.component.html page

<div class="container mt-5">

<h2>Angular Display Data from Json Example</h2>

<table class="table table-striped" border="1">
<thead>
<tr>
<th>Id</th>
<th>Name</th> 
<th>Email</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of Users">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td> 
<td>{{ user.email }}</td>
</tr>
</tbody>
</table>
</div>

<router-outlet></router-outlet>

18. What is the file extension of JSON?

File extension of JSON is .json

19. What is JSON-RPC and JSON Parser?

  • JSON RPC: It is a simple remote procedure call protocol same as XML-RPC although it uses the lightweight JSON format instead of XML
  • JSON Parser: JSON parser is used to parse the JSON data into objects to use its value. It can be parsed by javaScript, PHP and jQuery

20. Why use JSON instead of XML?

  • JSON objects are typed JSON types: Number, Array, Boolean, and String are all sorts of data. It is faster and lighter than XML since XML data is typeless over the wire.
  • In XML, everything is a string.
  • Data is readily available as a JSON object in your JavaScript.
  • You have to read from an object property in your JavaScript code to get values.

Prev Next