create json object in javascript – how to build json object in javascript?

create json object in javascript – JSON, short for JavaScript Object Notation To create an object we need to use opening and closing curly braces {} as well as Data is put in pairs of name/value.

create json object in javascript

A JSON string can be passed directly into JSON.parse() and it creates an appropriate JavaScript value.
Creating Simple Objects

//Creation of an empty Object −
var ProductOBJ = {};

//Creation of a new Object −
var ProductOBJ = new Object();

// Creation of an object with attribute product_title with value in string, attribute amount with numeric value
var ProductOBJ = { "product_title ":"VB BLACK BOOK", "amount":500 };

Creating Array Objects

<html>
   <head>
      <title>Creation of array object in javascript using JSON</title>
      <script language = "javascript" >
         document.writeln("<h2>JSON array object</h2>");
         var products = { "Mobile" : [ 
            { "Name"  : "Mobile Made Simple", "amount" : 700 },
            { "Name"  : "Guide to Mobile", "amount" : 400 }],  
				
            "Shops"  : [
               { "Name"  : "Shops for the Impatient", "amount" : 1000 }, 
               { "Name"  : "Shops in Depth", "amount" : 1300 }]    
         }    
         var i = 0
         document.writeln("<table border = '2'><tr>");
			
         for(i = 0;i<products.Mobile.length;i++) {	
            document.writeln("<td>");
            document.writeln("<table border = '1' width = 100 >");
            document.writeln("<tr><td><b>Name</b></td><td width = 50>" + products.Mobile[i].Name+"</td></tr>");
            document.writeln("<tr><td><b>Price</b></td><td width = 50>" + products.Mobile[i].amount +"</td></tr>");
            document.writeln("</table>");
            document.writeln("</td>");
         }

         for(i = 0;i<products.Shops.length;i++) {
            document.writeln("<td>");
            document.writeln("<table border = '1' width = 100 >");
            document.writeln("<tr><td><b>Name</b></td><td width = 50>" + products.Shops[i].Name+"</td></tr>");
            document.writeln("<tr><td><b>Price</b></td><td width = 50>" + products.Shops[i].amount+"</td></tr>");
            document.writeln("</table>");
            document.writeln("</td>");
         }
			
         document.writeln("</tr></table>");
      </script>
   </head>
   
   <body>
   </body>
</html>

create json object in javascript

object to json javascript

var product={"title":"Laptop z570","desc":"Great Lap","price":52685};
var personJSONString=JSON.stringify(product); 

create a json object in javascript

var products = {
  "productName": "Mobile L1502",
  "price": 35896
}

js create json from object

JSON.stringify(products);

Don’t Miss : Converting JSON Object Into Javascript PHP Array

Read Also:  how to upload video in php with example

create a javascript json object

const products = {
  name: "mobile",
  price: 2154,
  rank: 6
}

I hope you get an idea about create json object in javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.