0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Object to JSON String

Use JSON.stringify(yourObject) to convert a JavaScript object into a JSON string.
📋

Examples

Input{"name":"Alice"}
Output"{\"name\":\"Alice\"}"
Input{"age":30,"city":"New York"}
Output"{\"age\":30,\"city\":\"New York\"}"
Input{}
Output"{}"
🧠

How to Think About It

To convert an object to JSON, think of turning the object's data into a text format that can be easily shared or stored. JavaScript provides a built-in method to do this by taking the object and producing a string that follows JSON rules.
📐

Algorithm

1
Get the JavaScript object you want to convert.
2
Use the built-in method to turn the object into a JSON string.
3
Return or use the JSON string as needed.
💻

Code

javascript
const obj = { name: "Alice", age: 30 };
const jsonString = JSON.stringify(obj);
console.log(jsonString);
Output
{"name":"Alice","age":30}
🔍

Dry Run

Let's trace converting { name: "Alice", age: 30 } to JSON string.

1

Start with object

{ name: "Alice", age: 30 }

2

Call JSON.stringify

JSON.stringify({ name: "Alice", age: 30 })

3

Get JSON string

"{\"name\":\"Alice\",\"age\":30}"

StepValue
1{ name: "Alice", age: 30 }
2JSON.stringify called
3"{\"name\":\"Alice\",\"age\":30}"
💡

Why This Works

Step 1: Why use JSON.stringify

The JSON.stringify method converts an object into a string that follows JSON format, which is easy to send or save.

Step 2: How it works

It looks at each key and value in the object and creates a text version with quotes and commas as JSON requires.

Step 3: Result is a string

The output is a string, not an object, so you can store it or send it over the internet.

🔄

Alternative Approaches

Manual string building
javascript
const obj = { name: "Alice", age: 30 };
const jsonString = '{"name":"' + obj.name + '","age":' + obj.age + '}';
console.log(jsonString);
This works only for simple objects and is error-prone; JSON.stringify is safer and easier.
Using a replacer function in JSON.stringify
javascript
const obj = { name: "Alice", age: 30, password: "secret" };
const jsonString = JSON.stringify(obj, (key, value) => key === 'password' ? undefined : value);
console.log(jsonString);
This lets you exclude certain keys while converting, useful for filtering data.

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

Time Complexity

The time depends on the number of keys and values in the object, as each must be processed to create the JSON string.

Space Complexity

The space needed grows with the size of the object because the output is a new string representing the entire object.

Which Approach is Fastest?

Using JSON.stringify is the fastest and safest method compared to manual string building or custom replacers.

ApproachTimeSpaceBest For
JSON.stringifyO(n)O(n)All object sizes, safe and standard
Manual string buildingO(n)O(n)Very simple objects, not recommended
JSON.stringify with replacerO(n)O(n)Filtering keys during conversion
💡
Always use JSON.stringify to safely convert objects to JSON strings without errors.
⚠️
Trying to use toString() on an object instead of JSON.stringify, which does not produce valid JSON.