EN
TypeScript - serialize object to json
0
points
In TypeScript script it is possible to serialize object to json in following way.
JSON.stringify method example
interface Student {
name: string;
age: number;
todos: string[];
}
const studentObject: Student = {
name: 'John',
age: 25,
todos: ['Sleeping', 'Lectures', 'Classes', 'Shopping'],
};
const studentJson = JSON.stringify(studentObject);
console.log(studentJson);
Output:
{"name":"John","age":25,"todos":["Sleeping","Lectures","Classes","Shopping"]}
Note:
JSON.stringifymethod has been introduced in ES5.