Hi Friends 👋,
Welcome to Infinitbility ❤️!
Today, we are going to see how we can convert the object to JSON string in typescript, here we will use the built-in method JSON.stringify()
to convert data object to JSON string.
When you want to pass data to API or anywhere then you should stringify your JSON.
Here, we will create a sample object and then we use JSON.stringify()
to convert the object to a string.
So let’s start the code…
1interface User {2 id: number;3 name: string;4 domain?: string;5 age?: number;6 isActive?: boolean;7}89let user: User = {10 id: 1,11 name: "infinitbility",12 domain: "infinitbility.com",13 age: 2,14 isActive: 115};1617console.log(user);18console.log(typeof user);19let jsonString : string = JSON.stringify(user);20console.log(jsonString);21console.log(typeof jsonString);
When you run the above code you will get object data and object data type before stringify.
After stringify, it will show object data as a string and data type also string.
check out the output of the above example.

All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.