Syntax
JSON.stringify(value)
JSON.parse(jsonString)Examples
Basic Stringify and Parse
Converting between JavaScript objects and JSON strings.
const user = { name: "Alice", age: 25, active: true };
const jsonString = JSON.stringify(user);
console.log(jsonString); // '{"name":"Alice","age":25,"active":true}'
const parsedBack = JSON.parse(jsonString);
console.log(parsedBack.name); // "Alice"
console.log(typeof parsedBack); // "object"Pretty-Printing with Indentation
Formatting JSON output for readability using the third argument.
const data = { name: "Bob", roles: ["admin", "editor"] };
console.log(JSON.stringify(data, null, 2));
// {
// "name": "Bob",
// "roles": [
// "admin",
// "editor"
// ]
// }What Gets Dropped or Converted
JSON.stringify() cannot represent every JavaScript value.
const data = {
name: "Alice",
greet: function() { console.log("hi"); }, // dropped entirely
unset: undefined, // dropped entirely
missing: null, // kept as null
when: new Date() // converted to an ISO string
};
console.log(JSON.stringify(data));
// functions and undefined values disappear from the outputBest practices
- Always wrap JSON.parse() in a try/catch, since it throws an error on invalid or malformed JSON
- Use the third argument of JSON.stringify() (a number of spaces) for human-readable, indented output during debugging
- Remember functions, undefined, and symbols are silently omitted during stringification - do not rely on them surviving a round trip
- Be aware that Dates become strings after JSON.stringify(), and will need to be manually converted back with new Date() after parsing
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.Data Types
JavaScript has seven primitive data types - string, number, boolean, undefined, null, bigint, and symbol - plus the object type, which includes arrays, functions, and plain objects. JavaScript is dynamically typed, meaning a variable can hold any type and that type can change. The typeof operator reports a value's type at runtime.