How to Convert an Array to JSON in JavaScript

To convert an array to json in JavaScript, you can use the “JSON.stringify()” method. The JSON.stringify() is a built-in method that converts a JavaScript object or value to a JSON string.


JSON.stringify(obj, replacer, space)


The obj is a required parameter, the value to convert a string.

The replacer is an optional argument with a function or an array to transform the result.

The space is an optional parameter, either a String or a Number.

Return value

The stringify() method returns a JSON string.


Define an array and then convert it into json string.

let moneyheist = ["Tokyo", "Nairobi", "Berlin", "Oslo", "Moscow"]

let moneyheist_json = JSON.stringify(moneyheist)

console.log("After converting into JSON string")

console.log("Check its data type using typeof()")


After converting into JSON string

Check its data type using typeof()

To check the data type of a variable in JavaScript, use the typeof() function.

The moneyheist_json variable is now a string, ready to send to the server.

That’s it for this tutorial.

Leave a Comment