Tutorials › JavaScript Tutorial › JavaScript JSON

JavaScript JSON

10 min read · Lesson 19 of 23

JSON (JavaScript Object Notation) is a text format for storing and sending data. When your web page asks a server for a list of products, when an M-Pesa callback reports a payment, or when an app saves its settings, the data very often travels as JSON. It looks almost exactly like JavaScript objects and arrays – but it is just a string.

What JSON looks like

{
  "name": "Skope Digital Academy",
  "city": "Kisumu",
  "founded": 2024,
  "online": true,
  "courses": ["HTML", "CSS", "JavaScript"],
  "contact": { "email": "[email protected]", "phone": null }
}

JSON is stricter than JavaScript object syntax:

  • Property names must be in double quotes.
  • Strings must use double quotes – single quotes are not allowed.
  • Allowed values are: strings, numbers, true, false, null, arrays and objects.
  • No functions, no undefined, no comments, and no trailing commas.

JSON.stringify – object to text

JSON.stringify() turns a JavaScript value into a JSON string, ready to send or store.

Example JAVASCRIPT
const order = {
  id: 501,
  customer: "Wairimu",
  items: ["Avocado", "Mango"],
  paid: true,
};

const text = JSON.stringify(order);
console.log(text);
console.log(typeof text);
Result
{"id":501,"customer":"Wairimu","items":["Avocado","Mango"],"paid":true}
string
Try it Yourself »

Pretty printing

Pass a third argument to add indentation, which makes JSON much easier for humans to read.

Example JAVASCRIPT
const branch = { town: "Kisumu", staff: 12 };
console.log(JSON.stringify(branch, null, 2));
Result
{
  "town": "Kisumu",
  "staff": 12
}
Try it Yourself »

The second argument (null here) can be used to filter which properties are included. For example, JSON.stringify(order, ["id", "paid"]) keeps only those two keys.

What gets dropped

Values that JSON cannot represent are left out or converted:

Example JAVASCRIPT
const data = {
  name: "Test",
  helper: function () {},
  missing: undefined,
  when: new Date(Date.UTC(2025, 0, 15)),
};
console.log(JSON.stringify(data));
Result
{"name":"Test","when":"2025-01-15T00:00:00.000Z"}
Try it Yourself »

Functions and undefined disappear; dates become ISO date strings.

JSON.parse – text to object

JSON.parse() does the opposite: it reads a JSON string and builds the matching JavaScript value.

Example JAVASCRIPT
const response = '{"status":"success","amount":1500,"receipt":"QK7X9ABC12"}';

const payment = JSON.parse(response);
console.log(payment.status);
console.log(payment.amount + 100);
console.log(payment.receipt);
Result
success
1600
QK7X9ABC12
Try it Yourself »

After parsing, you have a real object again: you can use dot notation, do maths with the numbers, loop over arrays and so on.

Invalid JSON throws an error

If the text is not valid JSON, JSON.parse throws a SyntaxError. Data from outside your program can always be broken, so wrap parsing in try...catch (covered in Errors).

Example JAVASCRIPT
const bad = "{name: 'Kamau'}"; // unquoted key and single quotes

try {
  JSON.parse(bad);
  console.log("Parsed fine");
} catch (error) {
  console.log("Could not read the data:", error.name);
}
Result
Could not read the data: SyntaxError
Try it Yourself »
Warning: Never use eval() to read JSON. It runs the text as code, which is a serious security risk. JSON.parse is safe and fast.

Arrays of objects

Most real JSON data is a list of records. Once parsed, you can use the array methods you already know.

Example JAVASCRIPT
const json = `[
  {"name": "Tusker", "price": 250},
  {"name": "Soda", "price": 80},
  {"name": "Water", "price": 50}
]`;

const drinks = JSON.parse(json);
const total = drinks.reduce((sum, d) => sum + d.price, 0);
const cheap = drinks.filter((d) => d.price < 100).map((d) => d.name);

console.log(drinks.length);
console.log(total);
console.log(cheap.join(", "));
Result
3
380
Soda, Water
Try it Yourself »

Making a deep copy

A quick way to make a fully independent copy of plain data is to stringify and parse it again. The modern built-in structuredClone() does the same job more reliably, also keeping dates.

Example JAVASCRIPT
const original = { town: "Kitale", tags: ["farm", "dairy"] };
const copy = JSON.parse(JSON.stringify(original));

copy.tags.push("maize");
console.log(original.tags.length, copy.tags.length);
Result
2 3
Try it Yourself »

JSON in the browser

When you later fetch data from a server with fetch(), you will usually call response.json(), which does the JSON.parse step for you. The format and rules you learned here are exactly the same.

Tip: Paste confusing JSON into your code editor and format it, or use JSON.stringify(value, null, 2) to print data clearly while debugging.
Exercise

Convert the object into a JSON string.

const text = JSON.(user);
Exercise

Convert the JSON string back into an object.

const user = JSON.(text);
Sign in or create a free account to track progress, earn XP, badges and a certificate.