What is JSON in JavaScript?
Learn what JSON is, how JSON.stringify and JSON.parse work in JavaScript, supported data types, and why JSON is the standard API data format.
Expected Interview Answer
JSON, short for JavaScript Object Notation, is a lightweight, text-based data format used to represent structured data as key-value pairs and arrays, commonly for exchanging data between a server and a client. Despite its name, JSON is language-independent and supported natively by virtually every modern programming language.
JSON syntax requires double-quoted keys and string values, and supports only a limited set of types: strings, numbers, booleans, null, objects, and arrays, notably excluding functions, undefined, and Dates as native types. In JavaScript, JSON.stringify() converts a JavaScript object into a JSON-formatted string, while JSON.parse() converts a JSON string back into a live JavaScript object. Because JSON has no comments and stricter quoting rules than JavaScript object literals, a JSON string is not always valid JavaScript object syntax even though it looks similar. JSON is the standard payload format for REST APIs, configuration files, and browser localStorage because it is compact, human-readable, and easy to parse across language boundaries.
- Language-independent, universal data interchange format
- Lightweight and human-readable compared to XML
- Native parsing support via JSON.parse and JSON.stringify
- Standard format for REST API request and response bodies
- Widely supported for config files and browser storage
AI Mentor Explanation
JSON is like a standardized scorecard format that every cricket board worldwide agrees to read the same way, regardless of which country's broadcaster produced it. A local commentator can convert a live match into that scorecard format, and any other broadcaster can read it back into their own graphics system without confusion.
Step-by-Step Explanation
Step 1
Understand JSON's limited types
JSON supports only strings, numbers, booleans, null, objects, and arrays — no functions, undefined, or native Dates.
Step 2
Serialize with JSON.stringify
Converting a JavaScript object into a JSON string turns it into portable text ready to send or store.
Step 3
Send or store the string
The JSON string is transmitted over HTTP, saved to a file, or stored in localStorage as plain text.
Step 4
Parse with JSON.parse
The receiving side converts the JSON string back into a live JavaScript object it can work with directly.
Step 5
Handle unsupported types explicitly
Functions and undefined are silently dropped by stringify, and Dates become plain ISO strings, so you must reconstruct them manually after parsing.
What Interviewer Expects
- Explains JSON.stringify and JSON.parse correctly
- Knows which JavaScript types JSON can and cannot represent
- Understands JSON requires double-quoted keys and strings
- Can name JSON's common real-world uses (APIs, config, storage)
- Distinguishes JSON text from JavaScript object literal syntax
Common Mistakes
- Saying JSON supports comments like JavaScript does
- Forgetting JSON.stringify silently drops functions and undefined values
- Using single quotes for keys/strings in JSON, which is invalid
- Assuming Dates survive round-tripping through JSON unchanged
- Confusing a JSON string with a plain JavaScript object
Best Answer (HR Friendly)
“JSON is a simple, universal text format for organizing data, similar to a structured list of labeled information. It's the standard way websites and apps send data back and forth, because almost every programming language can read and write it easily.”
Code Example
const user = { name: 'Ada', age: 30, greet: function () {}, extra: undefined };
const json = JSON.stringify(user);
console.log(json); // {"name":"Ada","age":30}
// Note: greet and extra were silently dropped
const parsed = JSON.parse(json);
console.log(parsed.name); // Ada
console.log(typeof parsed); // objectFollow-up Questions
- What data types are not supported natively in JSON?
- Why does JSON.stringify silently drop functions and undefined values?
- How do you correctly serialize and deserialize Date objects with JSON?
- What's the difference between a JSON string and a JavaScript object literal?
- How does JSON.stringify's replacer parameter work?
MCQ Practice
1. What does JSON.stringify() do?
JSON.stringify() serializes a JavaScript value into its JSON text representation.
2. Which of these is NOT a valid native JSON data type?
JSON has no representation for functions; JSON.stringify silently omits function-valued properties.
3. What does JSON.parse('{"a":1}') return?
JSON.parse converts a valid JSON string into its corresponding live JavaScript object.
Flash Cards
What does JSON stand for? — JavaScript Object Notation — a lightweight, language-independent data interchange format.
What does JSON.stringify do? — Converts a JavaScript value into a JSON-formatted string.
What does JSON.parse do? — Converts a JSON string back into a live JavaScript value/object.
Name a type JSON cannot natively represent. — Functions (and undefined) — both are silently dropped during stringify.