Latest Code Tutorials

How to Convert Javascript Array to JSON

At some point in your time, whatever developer you are, you need to deal with JSON data. JSON stands for Javascript Object Notation. Exchange data between client and server is straightforward using JSON. In this example, we will see how to convert an array to json in JavaScript.

How to convert Javascript array to json

To convert an array to json in JavaScript, use the json.stringify() method. The JSON.stringify() method is a built-in method that converts an array to json, and then we can send the string to the server. The JSON.stringify() function allows us to take an Object or Array and create a JSON string out of it.

JSON.stringify() converts the value to JSON notation representing it. If a value has the toJSON() method, it’s responsible for defining what data will be serialized.

See the following example.

// app.js

let data = [
jsonData = JSON.stringify(data)
console.log(typeof jsonData === 'string')


➜  es git:(master) ✗ node app
➜  es git:(master) ✗

In the above code, we have defined a Javascript array with three values and then converted it into a JSON string using JSON.stringify() method.

Then we have printed JSON data in the console and check if the typeof jsonData is a string or not. Of course, it will return true because it is now JSON string ready to send the server via AJAX request. You can use the Axios library to send a network request.

JavaScript Object to JSON

We can use the same JSON.stringify() function to convert the Javascript object to json.

// app.js

let obj = {
  name: 'Krunal',
  rollno: 10,
  site: 'AppDividend'
jsonData = JSON.stringify(obj)
console.log(typeof jsonData === 'string')


➜  es git:(master) ✗ node app
➜  es git:(master) ✗

We have defined an object and then pass to JSON.stringify() method, and it converts from Javascript object to json string.

Often, you need to convert the JavaScript values like string, array, or object into JSON before AJAX POST request.

The JSON object is now part of most modern web browsers (IE 8 & above).


We can convert Javascript datatypes like Number, Array, Object to JSON with just one function, and it is beneficial when exchanging data between client and server.

That’s it for this conversion tutorial.

1 Comment
  1. Amit Parmar says

    thanks for the guide

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.