All TalkersCode Topics

Follow TalkersCode On Social Media

Create JSON Array In JavaScript

Last Updated : Jan 1, 2023

Create JSON Array In JavaScript

In this article we will show you the solution of create JSON array in JavaScript, let us understand JSON first. The JSON stands for JavaScript Object Notation. It is a lightweight data-exchanging format.

It is used basically with API. JSON is language-independent and supports all kinds of frameworks and languages.

Step By Step Guide On Create JSON Array In JavaScript :-

Now let us see how to create an array using JSON.

Method 1 -

Creating just one array.
   var Thomas = [ {
        " name ":" Thomas " ,
        " city ": " London " ,
        " age " : " 30 "
    },
]
    document.write("Name : "+Thomas.name+"<br>");
    document.write("city : "+Thomas.city+"<br>");
    document.write("age : "+Thomas.age+"<br>");
  1. Creating a variable and all the information in the array ( ex- name, city , age…)
  2. Using document.write to show the array

Method 2 -

Creating many array at once.

var array = [ {
        " name " : " Thomas " ,
        " city ": " London " ,
        " age " : " 30 "
    },
{
    " name " : " Peter " ,
        " city " : " NewYork " ,
        " age " : " 21 "
},
{
    " name " : " Mary " ,
        " city " : " LA " ,
        " age " : " 34 "
},
{
    " name ":" Leo " ,
        " city ": " chicago " ,
        " age " : " 50 "
}
]
    for(var c=0;c<3;c++){
        document.write(array[c].name + " 's name : " + array[c].name + "<br>");
        document.write(array[c].name + " 's city : " + array[c].city + "<br>");
        document.write(array[c].name + " 's age : " + array[c].age + "<br>");
    }
  1. Creating many arrays, containing information (ex- name, city ,age…)
  2. Here a for loop was created to write all the arrays.

Method 3 -

Representing array as a table.

var array = [ {
    " name ":" Thomas " ,
        " city ": " London " ,
        " age " : " 30 "
},
{
    " name ":" Mary " ,
        " city ": " LA " ,
        " age " : " 34 "
} ]
console.table(array);
  1. Here console.table() used to represent the array as the table.

Method 4 -

Push element in array.

var array = [ {
    " name ":" Thomas " ,
        " city ": " London " ,
        " age " : " 30 "
},
{
    " name ":" Mary " ,
        " city ": " LA " ,
        " age " : " 34 "
} ]
let family2 = {
    " name ":" Leo " ,
        " city ": " chicago " ,
        " age " : " 50 "
}
array.push(family2);
console.table(array);

Conclusion :-

Lastly, in conclusion, here we can say that with the help of this article create a JSON array in JavaScript.

I hope this article on create JSON array in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials