Stringify JSON object in typescript
To stringify json object in typescript, use JSON.stringify()
it will convert your json object to json string.
The JSON.stringify()
method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
1interface domain {2 name: string,3 domain: string,4}56const arr: Array<domain> = [7 {8 name: "infinitbility",9 domain: "infinitbility.com"10 },11 {12 name: "aguidehub",13 domain: "aguidehub.com"14 }15 ];1617// โ Convert array of object to string18const str: string = JSON.stringify(arr);19console.log(str); // ๐๏ธ '[{"name":"infinitbility","domain":"infinitbility.com"},{"name":"aguidehub","domain":"aguidehub.com"}]'
Note: The toString()
and join()
method only work for array of string, number not object.
I hope it helps you, 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.