JavaScript How to Convert JSON to Array Easily
Use
JSON.parse(jsonString) to convert a JSON string into a JavaScript array if the JSON represents an array, for example: const arr = JSON.parse('[1,2,3]');.Examples
Input'[1,2,3]'
Output[1, 2, 3]
Input'["apple", "banana", "cherry"]'
Output["apple", "banana", "cherry"]
Input'[]'
Output[]
How to Think About It
To convert JSON to an array in JavaScript, first check if the JSON string represents an array (starts with '['). Then use
JSON.parse() to turn the string into a real JavaScript array you can work with.Algorithm
1
Get the JSON string input.2
Check if the JSON string represents an array (starts with '[').3
Use JSON.parse() to convert the JSON string to a JavaScript array.4
Return or use the resulting array.Code
javascript
const jsonString = '["red", "green", "blue"]'; const array = JSON.parse(jsonString); console.log(array);
Output
[ 'red', 'green', 'blue' ]
Dry Run
Let's trace converting '["red", "green", "blue"]' to an array.
1
Input JSON string
'["red", "green", "blue"]'
2
Parse JSON string
JSON.parse('["red", "green", "blue"]')
3
Resulting array
["red", "green", "blue"]
| Step | Action | Value |
|---|---|---|
| 1 | Input JSON string | '["red", "green", "blue"]' |
| 2 | Parse JSON | ["red", "green", "blue"] |
| 3 | Output array | ["red", "green", "blue"] |
Why This Works
Step 1: JSON string format
The input must be a valid JSON string representing an array, which starts with [ and ends with ].
Step 2: Using JSON.parse()
JSON.parse() reads the JSON string and converts it into the matching JavaScript object or array.
Step 3: Result is a JavaScript array
After parsing, you get a real JavaScript array you can use like any other array in your code.
Alternative Approaches
Using eval()
javascript
const jsonString = '[1, 2, 3]'; const array = eval(jsonString); console.log(array);
This works but is unsafe and not recommended because eval() can run harmful code.
Manually parsing string
javascript
const jsonString = '[1,2,3]'; const array = jsonString.slice(1, -1).split(',').map(Number); console.log(array);
This only works for simple arrays of numbers and is less flexible than JSON.parse().
Complexity: O(n) time, O(n) space
Time Complexity
Parsing the JSON string takes time proportional to its length, so it is O(n) where n is the string size.
Space Complexity
The parsed array uses extra space proportional to the number of elements, so O(n) space is needed.
Which Approach is Fastest?
JSON.parse() is the fastest and safest method compared to manual parsing or eval().
| Approach | Time | Space | Best For |
|---|---|---|---|
| JSON.parse() | O(n) | O(n) | Safe and general JSON to array conversion |
| eval() | O(n) | O(n) | Works but unsafe, avoid in practice |
| Manual parsing | O(n) | O(n) | Simple numeric arrays only, less flexible |
Always use
JSON.parse() to safely convert JSON strings to arrays in JavaScript.Trying to use
JSON.parse() on a JSON string that does not represent an array will not give an array.