0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Array to JSON String

Use JSON.stringify(yourArray) to convert an array to a JSON string in JavaScript.
📋

Examples

Input[]
Output"[]"
Input[1, 2, 3]
Output"[1,2,3]"
Input["apple", "banana", "cherry"]
Output"[\"apple\",\"banana\",\"cherry\"]"
🧠

How to Think About It

To convert an array to JSON, think of turning the array into a string that looks like how arrays are written in JSON format. JavaScript provides a built-in method JSON.stringify that takes any value, including arrays, and returns a JSON string representation.
📐

Algorithm

1
Get the input array.
2
Use the built-in method to convert the array to a JSON string.
3
Return or print the JSON string.
💻

Code

javascript
const array = ["apple", "banana", "cherry"];
const jsonString = JSON.stringify(array);
console.log(jsonString);
Output
"[\"apple\",\"banana\",\"cherry\"]"
🔍

Dry Run

Let's trace converting ["apple", "banana", "cherry"] to JSON string.

1

Input array

["apple", "banana", "cherry"]

2

Convert using JSON.stringify

JSON.stringify(["apple", "banana", "cherry"])

3

Output JSON string

"[\"apple\",\"banana\",\"cherry\"]"

StepActionValue
1Input array["apple", "banana", "cherry"]
2Call JSON.stringify"[\"apple\",\"banana\",\"cherry\"]"
3Print output"[\"apple\",\"banana\",\"cherry\"]"
💡

Why This Works

Step 1: Why use JSON.stringify

The JSON.stringify method converts JavaScript values, including arrays, into a JSON-formatted string.

Step 2: How arrays are converted

Each element in the array is converted to its JSON representation and combined with commas inside square brackets.

Step 3: Result is a string

The output is a string that looks like JSON, which can be sent or stored easily.

🔄

Alternative Approaches

Manual loop and string concatenation
javascript
const array = [1, 2, 3];
let jsonString = '[' + array.map(item => '"' + item + '"').join(',') + ']';
console.log(jsonString);
This works but is error-prone and less efficient than JSON.stringify.
Using a library like Lodash (for complex cases)
javascript
import _ from 'lodash';
const array = [1, 2, 3];
const jsonString = JSON.stringify(array);
console.log(jsonString);
Lodash doesn't convert arrays to JSON but helps with deep cloning or manipulation before conversion.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the number of elements in the array because each element must be processed and converted to a string.

Space Complexity

The output string size grows with the array size, so space is proportional to the input array length.

Which Approach is Fastest?

Using JSON.stringify is optimized and faster than manual string building.

ApproachTimeSpaceBest For
JSON.stringifyO(n)O(n)All array to JSON conversions
Manual string concatenationO(n)O(n)Simple arrays but error-prone
Using librariesO(n)O(n)Complex data manipulation before conversion
💡
Always use JSON.stringify to safely convert arrays to JSON strings.
⚠️
Trying to convert arrays by concatenating strings manually often leads to errors and invalid JSON.