JavaScript Tutorial

JavaScript JSON

JSON is text. JSON.stringify turns an object into a string. JSON.parse turns the string back.

Text that looks like data

JSON stands for JavaScript Object Notation. It is a string: quotes, braces, brackets, and commas. Servers send it. localStorage stores it. Your script does not send a live object over the network — it sends text.

Two functions sit on the JSON object. stringify goes object to string. parse goes string to object. That is the whole round trip.

JSON.stringify

Pass a value. You get a string. Objects and arrays become JSON text. Numbers, booleans, andnull become their JSON forms. Strings get quotes and escaped characters.

Example

const task = {
  id: 1,
  title: "Read chapter",
  done: false,
};

const text = JSON.stringify(task);
console.log(text);
console.log(typeof text);

The log looks like an object, but typeof text is "string". You cannot write text.title. Parse first, or keep the original object.

Click Try it in JavaScript under an example. That opens/javascript/try — a live page and a console.

JSON.parse

Pass a JSON string. You get a JavaScript value. If the text is not valid JSON, parse throws — wrap it in try / catch when the string came from a user, storage, or a network.

Example

const text = '{"id":1,"title":"Read chapter","done":false}';
const task = JSON.parse(text);

console.log(task.title);
console.log(task.done);
console.log(typeof task);

JSON is stricter than JavaScript

Object keys must be in double quotes. Strings must use double quotes, not single quotes. Trailing commas are illegal. undefined, functions, and Date objects are not JSON values — stringify drops undefined fields and turns dates into strings.

Example

try {
  JSON.parse("{title: 'Ada'}");
} catch (err) {
  console.log(err.message);
}

try {
  JSON.parse('{"title": "Ada",}');
} catch (err) {
  console.log(err.message);
}

console.log(JSON.parse('{"title":"Ada"}').title);

The first two strings would be fine as JavaScript object literals. They are not JSON. The third string parses.

Pretty print

A second argument to stringify is a replacer (use null to skip it). A third argument is spaces for indent. That is for logs and files people will read, not for compact storage.

Example

const user = { name: "Lin", role: "editor" };
console.log(JSON.stringify(user, null, 2));

Where you will use it

PlaceDirection
localStoragestringify to save, parse to load
fetch + response.json()parse the body for you
Copying datastringify then parse makes a deep clone of JSON-safe values

response.json() on a fetch is parse under another name. Storage only keeps strings, so you stringify objects before setItem. Both chapters come later in this track.

What to remember

  • JSON is text, not a live object.
  • JSON.stringify object to string. JSON.parse string to object.
  • Keys and strings use double quotes. No trailing commas.
  • Parse can throw. Catch it when the text is not under your control.

Next: debugging — reading the console, the error line, and fixing one problem at a time.

FAQ: JavaScript JSON

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript json examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript json in this JavaScript JavaScript lesson (JavaScript JSON).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.