Storing text data into the database is one of the best legal practices in the web development world. JSON makes it possible to store JavaScript objects as text. Storing and fetching the JSON data is very easy. The data has to be in the form of strings. Let’s see how to convert the array to json string.
Javascript Array to JSON
To convert an array to json in JavaScript, use the JSON.stringify() method. The JSON.stringify() method converts a JavaScript object into a string. You can take an Object as an Array because everything in JavaScript is an Object.
To serialize the data to strings to store the data into a database or sending the data to the webserver, use the JSON.stringify() method. Arrays in JSON are almost the same as arrays in JavaScript.
Let’s see the syntax of JSON.stringify() method.
Syntax
JSON.stringify(obj, replacer, space)
Arguments
The obj is a required parameter which is the value to convert a string.
The replacer is an optional argument with either a function or an array used to transform the result.
The space is an optional parameter which is either a String or a Number.
Return value
The stringify() method returns a JSON string.
Implementation of JSON.stringify()
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(moneyheist_json)
console.log("Check its data type using typeof()")
console.log(typeof(moneyheist_json))
Output
After converting into JSON string
["Tokyo","Nairobi","Berlin","Oslo","Moscow"]
Check its data type using typeof()
string
To check the data type of a variable in JavaScript, use the typeof() function.
The moneyheist_json variable is now a string, and it is ready to send to the server.
That’s it for this tutorial.
See also

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Front-end and backend technologies including React, Angular, and Vue.js and he is an expert in JavaScript Language.