Languages
[Edit]
EN

TypeScript - serialize object to json

0 points
Created by:
Welsh
902

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.stringify method has been introduced in ES5.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join