JavaScript – Convert Array to JSON String

To convert an array to JSON string in JavaScript, call JSON.stringify() method and pass the array as argument.

Syntax

The syntax to call JSON.stringify() method with array arr passed as argument is

</>
Copy
JSON.stringify(arr)

stringify() returns a string value.

Examples

In the following example, we take an array arr with strings as elements and convert this array to JSON string using JSON.stringify() method.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<body>
    <pre id="output"></pre>
    <script>
        var arr = ['apple', 'banana', 'orange'];
        var jsonString = JSON.stringify(arr);
        document.getElementById('output').innerHTML += jsonString;
    </script>
</body>
</html>

Conclusion

In this JavaScript Tutorial, we learned how to convert an array into JSON string in JavaScript using JSON.stringify() method, with examples.