跳到主要内容

JSON

JSON = JavaScript Object Notation for data exchange


What is JSON?

"Data format based on JavaScript objects"

JSON (JavaScript Object Notation) is a lightweight data interchange format.

"JSON is a text-based data format derived from JavaScript object syntax, used for data exchange between systems."


JSON Syntax

Similar to JavaScript objects

{
"name": "John",
"age": 30,
"isActive": true,
"hobbies": ["reading", "coding"],
"address": {
"city": "New York",
"country": "USA"
}
}

"JSON syntax is similar to JavaScript objects but with stricter rules - keys must be strings in double quotes."


JSON vs JavaScript Objects

JSON = subset, stricter rules

FeatureJSONJavaScript
KeysMust be stringsCan be identifiers
QuotesDouble quotes onlySingle or double
ValuesLimited typesAny JavaScript value
CommentsNot allowedAllowed
FunctionsNot allowedAllowed

"JSON is a subset of JavaScript - stricter rules, no functions, no comments, keys must be double-quoted strings."


JSON.parse()

Convert JSON string to object

const jsonString = '{"name":"John","age":30}';
const obj = JSON.parse(jsonString);
console.log(obj.name); // "John"

"JSON.parse converts a JSON string into a JavaScript object."


JSON.stringify()

Convert object to JSON string

const obj = { name: "John", age: 30 };
const jsonString = JSON.stringify(obj);
console.log(jsonString); // '{"name":"John","age":30}'

"JSON.stringify converts a JavaScript object into a JSON string."


Supported Data Types

Primitives + objects + arrays

JSON supports:

  • Strings
  • Numbers
  • Booleans
  • null
  • Objects
  • Arrays

Not supported:

  • Functions
  • undefined
  • Symbols
  • Dates (become strings)

"JSON supports primitives, objects, and arrays, but not functions, undefined, symbols, or Date objects."


Common Use Cases

API communication · Storage · Configuration

  • API requests/responses: Data exchange
  • localStorage: Store data as JSON
  • Configuration files: JSON configs
  • Data serialization: Convert objects to strings

"JSON is used for API communication, localStorage storage, configuration files, and data serialization."


Replacer Function

Filter properties in stringify

const obj = { name: "John", age: 30, password: "secret" };

const json = JSON.stringify(obj, (key, value) => {
if (key === "password") return undefined; // Exclude
return value;
});

"JSON.stringify accepts a replacer function to filter or transform properties during serialization."


9️⃣ Reviver Function

Transform values in parse

const json = '{"date":"2023-01-01"}';

const obj = JSON.parse(json, (key, value) => {
if (key === "date") return new Date(value);
return value;
});

"JSON.parse accepts a reviver function to transform values during parsing."


Error Handling

Try-catch for invalid JSON

try {
const obj = JSON.parse(invalidJson);
} catch (error) {
console.error("Invalid JSON:", error);
}

"Always use try-catch when parsing JSON, as invalid JSON throws errors."


"JSON is a data format based on JavaScript objects, used for data exchange. It has stricter rules - double-quoted keys, no functions or comments. JSON.parse converts strings to objects, JSON.stringify converts objects to strings. JSON supports primitives, objects, and arrays. Use replacer/reviver functions for transformation. Always handle parsing errors."


🧠 Ultra-Short Cheat Sheet

Data interchange format
JSON.parse (string → object)
JSON.stringify (object → string)
Stricter than JavaScript
No functions, undefined, dates
API communication
localStorage
Error handling