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:
stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The value to convert to a JSON string. |
replacer | optional | 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. |
space | optional | A 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
.
stringify(value: any, replacer?: (string | number)[] | null, space?: string | number): string
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | The value to convert to a JSON string. |
replacer | optional | 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. |
space | optional | A 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,
- Define an object
obj
with some properties. - Use the
stringify()
method to convert the object to a JSON string. - Store the result in the variable
jsonString
. - Log
jsonString
to the console using theconsole.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,
- Define an object
obj
with some properties. - Define a replacer function that filters out properties based on a condition.
- Use the
stringify()
method with the replacer function to convert the object to a JSON string. - Store the result in the variable
jsonString
. - Log
jsonString
to the console using theconsole.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,
- Define an object
obj
with some properties. - Use the
stringify()
method with the space parameter to convert the object to a readable JSON string. - Store the result in the variable
jsonString
. - Log
jsonString
to the console using theconsole.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.