JSON.stringify() with replacer

ES5+

Uses a replacer function to filter or transform values during JSON stringification.

Syntax

JSON.stringify(value, replacer)

Parameters

value any

The value to convert to a JSON string

replacer Function

A function that receives key and value, and returns the replacement value

Return Value

string

A JSON string with transformed values

Examples

JavaScript
const obj = {
  name: 'John',
  password: 'secret123',
  age: 30
};
const result = JSON.stringify(obj, (key, value) => {
  if (key === 'password') return undefined;
  return value;
});
console.log(result);
Output:
// '{"name":"John","age":30}'

Related Methods