TypeScript JSON stringify()
Syntax & Examples


JSON.stringify() method

The stringify() method in the JSON class converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or including only the specified properties if a replacer array is specified.


Syntax of JSON.stringify()

There are 2 variations for the syntax of JSON.stringify() method. They are:

1.
stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string

Parameters

ParameterOptional/RequiredDescription
valuerequiredThe value to convert to a JSON string.
replaceroptionalA function that alters the behavior of the stringification process, or an array of strings and numbers that acts as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string.
spaceoptionalA string or number that's used to insert white space into the output JSON string for readability purposes.

This method converts a JavaScript value to a JSON string. The replacer parameter can be a function that alters the behavior of the stringification process or an array of strings and numbers that acts as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. The space parameter can be used to control spacing in the final string.

Returns value of type string.

2.
stringify(value: any, replacer?: (string | number)[] | null, space?: string | number): string

Parameters

ParameterOptional/RequiredDescription
valuerequiredThe value to convert to a JSON string.
replaceroptionalA function that alters the behavior of the stringification process, or an array of strings and numbers that acts as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string.
spaceoptionalA string or number that's used to insert white space into the output JSON string for readability purposes.

This method converts a JavaScript value to a JSON string. The replacer parameter can be a function that alters the behavior of the stringification process or an array of strings and numbers that acts as a whitelist for selecting/filtering the properties of the value object to be included in the JSON string. The space parameter can be used to control spacing in the final string.

Returns value of type string.



✐ Examples

1 Using stringify() with a simple object

In TypeScript, you can use the stringify() method to convert a simple object to a JSON string.

For example,

  1. Define an object obj with some properties.
  2. Use the stringify() method to convert the object to a JSON string.
  3. Store the result in the variable jsonString.
  4. Log jsonString to the console using the console.log() method.

TypeScript Program

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

Output

{"name":"John","age":30,"city":"New York"}

2 Using stringify() with a replacer function

In TypeScript, you can use the stringify() method with a replacer function to filter out properties during the conversion.

For example,

  1. Define an object obj with some properties.
  2. Define a replacer function that filters out properties based on a condition.
  3. Use the stringify() method with the replacer function to convert the object to a JSON string.
  4. Store the result in the variable jsonString.
  5. Log jsonString to the console using the console.log() method.

TypeScript Program

const obj = { name: 'John', age: 30, city: 'New York' };
const replacer = (key, value) => key === 'city' ? undefined : value;
const jsonString = JSON.stringify(obj, replacer);
console.log(jsonString);

Output

{"name":"John","age":30}

3 Using stringify() with space parameter for readability

In TypeScript, you can use the stringify() method with the space parameter to add indentation and make the JSON string more readable.

For example,

  1. Define an object obj with some properties.
  2. Use the stringify() method with the space parameter to convert the object to a readable JSON string.
  3. Store the result in the variable jsonString.
  4. Log jsonString to the console using the console.log() method.

TypeScript Program

const obj = { name: 'John', age: 30, city: 'New York' };
const jsonString = JSON.stringify(obj, null, 2);
console.log(jsonString);

Output

{
  "name": "John",
  "age": 30,
  "city": "New York"
}

Summary

In this TypeScript tutorial, we learned about stringify() method of JSON: the syntax and few working examples with output and detailed explanation for each example.