All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Create json Object

Last Updated : Jul 1, 2023

JavaScript Create json Object

In this article we will show you the solution of JavaScript create json object, there are many ways with help of which we are able to create JSON object inside JavaScript.

For a better understanding, let us see the example given below.

Step By Step Guide On JavaScript Create json Object :-

First question arises here that why we need to create JSON object in JavaScript.

This is because to use array in our codes again we have to stringify the array.

So, without doing that we can create JSON object directly. Below we are going to show an example in which we understand how to create JSON object in JavaScript.

So, without wasting more time let us see the codes for better understanding.

<!DOCTYPE html>
<html>
<head>
<title>
JavaScript create JSON object
</title>
</head>
<body>
<h1>
TalkersCode – JavaScript Tutorials
</h1>
<h2>
JavaScript create JSON object
</h2>
<script>
let data1 = {};
let data2 = [];
data1.data2 = data2;
console.log(data1);
let email = "admin@talkerscode.com";
let password= "admin@123456";
let data = {
email,
password,
};
data1.data2.push(data);
console.log(JSON.stringify(data1));
</script>
</body>
</html>
  1. As we here see that we today write only JavaScript codes in our example.
  2. In previous tutorials, we already many times create a full code example in which we first create an HTML form with its basic structure. Inside that structure, we use HTML, head, title, body, and script tags.
  3. Now, one thing to note here that JavaScript codes are always written under script tag. Whereas script tag is a paired tag. So, let us see the above codes given in script tag to see how to create JSON object.
  4. Now, inside the example given above. We see that we create one object and one array. After that we create two variable and store some data in them. As you see we create here email and password. After that as you see we just push the data in them.
  5. But before this all, one thing to note here that we just get the reference of data2 into data1 and store data2 in them. You can also see the console to see the output of this. At next after we push the data into our data1 and data2. Now, at last we want to create JSON object in this article. So, after push of data we use JSON stringify for conversion of data into string.
  6. And here we console the data1 to see the output. So, as result our data1 get displayed. So, in this way we are able to create JSON object in this article and the output displayed is given as: {"data2":[{"email":"admin@talkerscode.com","password":"admin@123456"}]}

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand how create JSON object using JavaScript.

I hope this tutorial on JavaScript create json object helps you and the steps and method mentioned above are easy to follow and implement.