All posts
JavaScriptNovember 10, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

What is JSON?

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:

  • Keys (the names) must be wrapped in double quotes.
  • Strings must use double quotes, never single quotes.
  • No trailing commas after the last item.
  • Allowed values: strings, numbers, true, false, null, arrays, and objects. Functions and undefined are not allowed.
js
{
  "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.

Object vs JSON string: the key difference

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 objectJSON string
What it isLive data your code can usePlain text that describes data
Read a valueuser.name worksDoes not work
Send over the networkNo, convert it firstYes
Convert withJSON.stringify()JSON.parse()

JSON.stringify: object to text

Stringify means turn into a string. Use it when you want to send or save an object.

js
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.

Make the output easy to read

By default the text is squeezed onto one line. Pass extra arguments to add indentation. This is great for debugging.

js
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.

JSON.parse: text to object

Parse means read and understand. JSON.parse takes JSON text and builds a real JavaScript object from it.

js
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.

A real example: saving data in localStorage

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.

js
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 guide

JSON and the Fetch API

When 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.

js
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 guide

Common errors and how to fix them

Unexpected token in JSON

If 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.

js
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.

Things JSON.stringify drops

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.

js
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.

Quick summary

  • JSON is a text format for data with strict quote rules.
  • JSON.stringify(object) gives you text.
  • JSON.parse(text) gives you an object.
  • Use both with localStorage, and use stringify when sending data with fetch.
  • Wrap JSON.parse in try...catch when the text is not fully under your control.

Frequently asked questions

What does JSON.stringify do in JavaScript?+

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.

What does JSON.parse do?+

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.

Is JSON the same as a JavaScript object?+

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.

Why do I get Unexpected token in JSON at position 0?+

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.

Do I need JSON.parse when using fetch?+

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 Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.