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.
interface domain {
name: string,
domain: string,
}
const arr: Array<domain> = [
{
name: "infinitbility",
domain: "infinitbility.com"
},
{
name: "aguidehub",
domain: "aguidehub.com"
}
];
// ✅ Convert array of object to string
const str: string = JSON.stringify(arr);
console.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 👍.