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.
xxxxxxxxxx
1
const text: string = 'This is example text...';
2
const json: string = JSON.stringify(text);
3
4
console.log(json);
In this example, we print object using JSON.stringify
.
xxxxxxxxxx
1
interface Student {
2
name: string;
3
age: number;
4
}
5
6
const studentObject: Student = {
7
name: 'John',
8
age: 25,
9
};
10
11
const studentJson: string = JSON.stringify(studentObject);
12
13
console.log(studentJson); // {"name":"John","age":25}