🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
JavaScript

JSON

📚 What is JSON? JSON (JavaScript Object Notation) is the universal language of data on the web. When your app talks to an API, data travels as a JSON string. JSON.stringify() converts a JavaScript object into a JSON string for sending. JSON.parse() converts it back into an object after receiving.

8 min 10 XP Lesson 15 of 21
JSON
🌐

Appy Says…

JSON is the universal language of the internet. Every API — Twitter, Spotify, YouTube, weather, maps — sends data as JSON. If you can read and write JSON in JavaScript, you can connect your app to anything on the web.

📖

What is JSON?

JSON (JavaScript Object Notation) is a text format for representing structured data. It looks almost identical to a JavaScript object, but it has strict rules: keys must be double-quoted strings, and only certain value types are allowed.

  • Valid JSON types: string, number, boolean, null, array, object
  • NOT valid in JSON: undefined, functions, Date, Symbol
  • Parse JSON string → JS object: JSON.parse(jsonString)
  • Convert JS object → JSON string: JSON.stringify(jsObject)
  • Pretty print: JSON.stringify(obj, null, 2) (indent 2 spaces)
  • JSON keys are always double-quoted: {"name": "Appy"}
🎮

Think of it like a universal game save format

Imagine every game could read each other's save files because they all used the same format. JSON is that universal format for the web — no matter what language the server uses (Python, Ruby, Java), it speaks JSON and so does your browser.

⚙️

How It Works

  • 1. Server sends a HTTP response with body: {"user": "Appy", "score": 95}
  • 2. const data = JSON.parse(responseText) converts it to a JavaScript object
  • 3. Access normally: data.user, data.score
  • 4. To send data: JSON.stringify({ action: 'like', postId: 42 })
  • 5. fetch with .json() does the parse automatically
  • 6. Wrap JSON.parse in try/catch — invalid JSON throws a SyntaxError
🌍

Real-World Examples

  • Spotify's API returns your playlists as a JSON array of objects
  • TikTok's For You feed is a JSON array of video metadata objects
  • Weather apps fetch JSON with temperature, humidity, and forecast data
  • WhatsApp stores your chat backup as encrypted JSON
  • Vite/npm config files (package.json) are JSON — you edit them constantly
💡

Key Facts

  • JSON was invented by Douglas Crockford in the early 2000s and is now used by virtually every web API
  • JSON.stringify() skips undefined values and functions — they simply disappear
  • A Date object stringifies to an ISO string: "2025-01-15T10:30:00.000Z"
  • JSON.parse(JSON.stringify(obj)) is a quick deep clone — but loses Dates and functions
⚠️

Watch Out!

JSON has no undefined — properties with undefined values are silently dropped during JSON.stringify(). If you send { score: undefined }, the server receives {}. Use null for intentional absence of a value.

📌

Remember

JSON.parse(str) → JavaScript object. JSON.stringify(obj) → JSON string. Always wrap JSON.parse in try/catch.

What You Learned

  • JSON is the universal text format for APIs; parse with JSON.parse(), stringify with JSON.stringify()
  • JSON has no undefined, functions, or Dates — only 6 types
  • Unlocks: consuming any web API, sending data to servers, config files

Key Facts

  • JSON was invented by Douglas Crockford in the early 2000s and is now used by virtually every web API
  • JSON.stringify() skips undefined values and functions — they simply disappear
  • A Date object stringifies to an ISO string: "2025-01-15T10:30:00.000Z"
  • JSON.parse(JSON.stringify(obj)) is a quick deep clone — but loses Dates and functions

Real-World Examples

• Spotify's API returns your playlists as a JSON array of objects • TikTok's For You feed is a JSON array of video metadata objects • Weather apps fetch JSON with temperature, humidity, and forecast data • WhatsApp stores your chat backup as encrypted JSON • Vite/npm config files (<code>package.json</code>) are JSON — you edit them constantly

Remember

JSON.parse(str) → JavaScript object. JSON.stringify(obj) → JSON string. Always wrap JSON.parse in try/catch.

Quick Quiz

1 / 2

JSON.stringify() does?