JavaScript JSON.parse and JSON.stringify for Beginners
Learn what JSON is and how JSON.parse and JSON.stringify work in JavaScript. Simple examples, line-by-line explanations, and common errors for beginners.
Learn what JSON is and how JSON.parse and JSON.stringify work in JavaScript. Simple examples, line-by-line explanations, and common errors for beginners.
What are JSON.parse and JSON.stringify in JavaScript? In plain words: JSON.stringify turns a JavaScript object into a piece of text, and JSON.parse turns that text back into a real JavaScript object. You need them whenever you send data to a server, get data from an API, or save data in the browser.
This guide assumes you know nothing about JSON. We will define every term, build up with small examples you can copy into your browser console, and finish with the most common mistakes.
JSON stands for JavaScript Object Notation. It is a text format for writing down data. Because it is just text, it can travel over the internet and be stored in files. Almost every web API (a service that lets programs ask for data) speaks JSON.
JSON looks very similar to a JavaScript object, but it has stricter rules:
{
"name": "Maria",
"age": 28,
"isStudent": false,
"skills": ["HTML", "CSS", "JavaScript"]
}That is valid JSON. It describes a person with a name, an age, a true/false value, and a list of skills. Notice every key and string uses double quotes.
This is where beginners get confused. A JavaScript object is something your code can work with: you can read user.name. A JSON string is only text. You cannot read .name from text. You must convert it first.
| JavaScript object | JSON string | |
|---|---|---|
| What it is | Live data your code can use | Plain text that describes data |
| Read a value | user.name works | Does not work |
| Send over the network | No, convert it first | Yes |
| Convert with | JSON.stringify() | JSON.parse() |
Stringify means turn into a string. Use it when you want to send or save an object.
const user = {
name: "Maria",
age: 28,
skills: ["HTML", "CSS"]
};
const text = JSON.stringify(user);
console.log(text);
console.log(typeof text);Line by line: the first block creates a normal object called user. Then JSON.stringify(user) converts it into text and stores it in text. The first console.log prints {"name":"Maria","age":28,"skills":["HTML","CSS"]}. The second prints string, which proves the result is text and no longer an object.
By default the text is squeezed onto one line. Pass extra arguments to add indentation. This is great for debugging.
const pretty = JSON.stringify(user, null, 2);
console.log(pretty);The second argument (null) is an advanced filter we skip for now. The third argument, 2, means indent each level by two spaces. The output is now spread over several readable lines.
Parse means read and understand. JSON.parse takes JSON text and builds a real JavaScript object from it.
const text = '{"name":"Maria","age":28}';
const user = JSON.parse(text);
console.log(user.name);
console.log(user.age + 1);The first line holds JSON text inside single quotes (the outer quotes belong to JavaScript, the inner double quotes belong to JSON). JSON.parse(text) turns it into an object. Now user.name prints Maria, and user.age + 1 prints 29 because age is a real number.
The browser's localStorage can only store strings. So to save an object or array, you stringify it first, and parse it when you read it back.
const settings = { theme: "dark", fontSize: 16 };
// Save
localStorage.setItem("settings", JSON.stringify(settings));
// Load
const saved = JSON.parse(localStorage.getItem("settings"));
console.log(saved.theme);We stringify settings and store it under the key "settings". Later, getItem returns the text, and JSON.parse turns it back into an object so saved.theme prints dark. Without these two functions, you would just see the useless text [object Object].
New to saving data in the browser? Read our beginner guide to localStorage first.
JavaScript localStorage guideWhen you call an API with fetch, the server sends JSON text. The response.json() method runs the parsing for you. When you send data, you stringify it yourself.
async function createUser() {
const response = await fetch("https://example.com/api/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "Maria" })
});
const data = await response.json();
console.log(data);
}We send a POST request. The headers line tells the server the body is JSON. The body is our object turned into text with JSON.stringify. When the answer arrives, response.json() parses the returned JSON text into an object we store in data.
Want the full story on calling APIs? See our Fetch API guide.
JavaScript Fetch API guideIf the text is not valid JSON, JSON.parse throws an error such as Unexpected token ' in JSON at position 1. The usual causes are single quotes, missing quotes around keys, or a trailing comma.
const bad = "{'name': 'Maria'}";
try {
const user = JSON.parse(bad);
} catch (error) {
console.log("Invalid JSON:", error.message);
}The text uses single quotes, so it is not valid JSON. Wrapping JSON.parse in try...catch stops your program from crashing and lets you show a friendly message instead. Always do this when the text comes from outside your code.
Functions and undefined values are removed from objects. Dates become text. Check the result when your data is more than simple strings, numbers, arrays, and objects.
const item = {
name: "Lamp",
secret: undefined,
sayHi() { return "hi"; }
};
console.log(JSON.stringify(item));This prints {"name":"Lamp"}. The secret and sayHi properties are gone because JSON cannot hold undefined or functions.
It converts a JavaScript value, usually an object or array, into a JSON string. You need this to send data to a server or save it in localStorage. Functions and undefined values are left out.
It reads a JSON string and turns it into a JavaScript object, array, or value. After parsing you can use dot notation such as user.name. If the text is invalid JSON, it throws an error.
No. A JavaScript object is live data inside your program, while JSON is plain text that only looks similar. JSON also has stricter rules, such as requiring double quotes around keys.
It means the text you passed to JSON.parse is not valid JSON. Common causes are single quotes, an empty string, or passing HTML or [object Object] instead of JSON. Log the text first to see what it really contains.
Not directly. The response.json() method already parses the JSON for you. You only use JSON.parse yourself for text from other places, such as localStorage.
Learning to code and want a project built, or need a hand with your website? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.