In this tutorial, We will explain some important JSON interview questions and answers.
The below following points defines the JSON.
JSON is based on two types of structures:
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.
3. What are the advantages of JSON?
The below following points defines JSON advantages.
4. What are the disadvantages of JSON?
The below following points defines JSON Disadvantages.
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. |
3 | 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.
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
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.
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.
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.
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?
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
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?
20. Why use JSON instead of XML?