Categories
JavaScript Answers

How to turn TypeScript object into JSON string?

Spread the love

Sometimes, we want to turn TypeScript object into JSON string.

In this article, we’ll look at how to turn TypeScript object into JSON string.

How to turn TypeScript object into JSON string?

To turn TypeScript object into JSON string, we can use the JSON.stringify method.

For instance, we write

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

to call JSON.stringify with the jsObj object to convert it to a JSON string.

Conclusion

To turn TypeScript object into JSON string, we can use the JSON.stringify method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *