How to Convert to JSON in JavaScript
Use JSON.stringify() when JavaScript objects or arrays need to become a JSON string for APIs, local storage, logging, or file export. This is the standard way to serialize data.
What you’ll build or solve
You’ll learn how to convert to JSON in JavaScript using JSON.stringify(). You’ll also know how to format readable JSON and avoid unsupported values.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when structured JavaScript data needs to leave memory as text.
Common real-world scenarios include:
- API POST bodies
- Local storage
- Config export
- Debug logging
- File downloads
This is a bad idea when the data should stay as a live object for immediate code logic.
Prerequisites
You only need:
- A JavaScript file or browser console
- A basic object or array
- Basic object syntax knowledge
Step-by-step instructions
Step 1: Pass the object into JSON.stringify()
Start with a JavaScript object.
JavaScript
const user = {
name: "Alex",
role: "admin"
};
Use JSON.stringify() to convert it into a JSON string.
JavaScript
const json = JSON.stringify(user);
console.log(json);
This also works for arrays.
JavaScript
const skills = ["HTML", "CSS", "JavaScript"];
const jsonSkills = JSON.stringify(skills);
For readable formatting, add spacing arguments.
JavaScript
const prettyJson = JSON.stringify(user, null, 2);
console.log(prettyJson);
What to look for:
- Input is a JavaScript object or array
- Output is a JSON string
- Great for APIs and local storage
- Pretty formatting helps debugging
- Functions and
undefinedare skipped
Examples you can copy
API request body
JavaScript
const body = JSON.stringify({
email: "alex@example.com"
});
Save settings
JavaScript
localStorage.setItem("settings", JSON.stringify(settings));
Debug export
JavaScript
console.log(JSON.stringify(data, null, 2));
Common mistakes and how to fix them
Mistake 1: Forgetting to stringify before local storage
What the reader might do:
JavaScript
localStorage.setItem("user", user);
Why it breaks: local storage stores strings, so the object becomes [object Object].
Corrected approach:
JavaScript
localStorage.setItem("user", JSON.stringify(user));
Mistake 2: Expecting functions to survive
What the reader might do:
JavaScript
const user = {
name: "Alex",
greet() {}
};
Why it breaks: functions are omitted from JSON output.
Corrected approach:
Only serialize data, not behavior.
Mistake 3: Using the JSON string like an object
What the reader might do:
JavaScript
const json = JSON.stringify(user);
console.log(json.name);
Why it breaks: the result is a string.
Corrected approach:
Use the original object, or parse the string later.
Troubleshooting
If local storage shows [object Object], add JSON.stringify().
If properties disappear, check for functions or undefined.
If the output is hard to read, use the spacing argument.
If later code needs object access, parse the string back with JSON.parse().
Quick recap
- Use
JSON.stringify()on objects or arrays - The result is a JSON string
- Great for APIs and storage
- Use spacing for readable debug output
- Parse back later when object access is needed
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot