JavaScript How to Convert JSON String to Object
Use
JSON.parse(jsonString) to convert a JSON string into a JavaScript object.Examples
Input"{\"name\":\"Alice\",\"age\":25}"
Output{"name":"Alice","age":25}
Input"[1,2,3,4]"
Output[1,2,3,4]
Input"true"
Outputtrue
How to Think About It
To convert a JSON string to an object, you need to use a built-in method that reads the string and creates the matching JavaScript data structure. The method reads the string format and turns it into an object, array, or primitive value depending on the content.
Algorithm
1
Get the JSON string input.2
Use the built-in JSON parsing method to read the string.3
Return the resulting JavaScript object or value.Code
javascript
const jsonString = '{"name":"Alice","age":25}'; const obj = JSON.parse(jsonString); console.log(obj); console.log(obj.name); console.log(obj.age);
Output
{"name":"Alice","age":25}
Alice
25
Dry Run
Let's trace converting '{"name":"Alice","age":25}' to an object.
1
Input JSON string
{"name":"Alice","age":25}
2
Parse string with JSON.parse
Converts string to { name: 'Alice', age: 25 }
3
Access object properties
obj.name is 'Alice', obj.age is 25
| Step | Action | Value |
|---|---|---|
| 1 | Input string | {"name":"Alice","age":25} |
| 2 | After JSON.parse | {"name":"Alice","age":25} (object) |
| 3 | Access obj.name | Alice |
| 4 | Access obj.age | 25 |
Why This Works
Step 1: Parsing JSON string
The JSON.parse method reads the JSON string and converts it into a JavaScript object or value.
Step 2: Creating JavaScript object
The method creates an object with keys and values matching the JSON string structure.
Step 3: Using the object
You can now access properties like obj.name or obj.age as normal JavaScript object properties.
Alternative Approaches
Using eval (not recommended)
javascript
const jsonString = '{"name":"Alice","age":25}'; const obj = eval('(' + jsonString + ')'); console.log(obj);
This works but is unsafe and can run malicious code, so avoid using eval for JSON parsing.
Using a try-catch block with JSON.parse
javascript
const jsonString = '{"name":"Alice","age":25}'; let obj; try { obj = JSON.parse(jsonString); console.log(obj); } catch (e) { console.error('Invalid JSON string'); }
This safely handles invalid JSON strings by catching errors.
Complexity: O(n) time, O(n) space
Time Complexity
Parsing the JSON string requires reading each character once, so it takes linear time relative to the string length.
Space Complexity
The method creates a new object in memory proportional to the size of the JSON string.
Which Approach is Fastest?
Using JSON.parse is the fastest and safest built-in method. Alternatives like eval are slower and unsafe.
| Approach | Time | Space | Best For |
|---|---|---|---|
| JSON.parse | O(n) | O(n) | Safe and fast JSON string to object conversion |
| eval | O(n) | O(n) | Unsafe, avoid for JSON parsing |
| Try-catch with JSON.parse | O(n) | O(n) | Safe parsing with error handling |
Always use
JSON.parse to safely convert JSON strings to objects.Trying to use
JSON.stringify instead of JSON.parse to convert JSON strings to objects.