EN
TypeScript - convert string variable to json text
0
points
In TypeScript it is possible to convert string constiable to json text in following way.
1. JSON.stringify
example
Example 1
const text: string = 'This is example text...';
const json: string = JSON.stringify(text);
console.log(json);
Example 2
In this example, we print object using JSON.stringify
.
interface Student {
name: string;
age: number;
}
const studentObject: Student = {
name: 'John',
age: 25,
};
const studentJson: string = JSON.stringify(studentObject);
console.log(studentJson); // {"name":"John","age":25}