Sanket Deshpande Sanket Deshpande - 4 years ago 360
Javascript Question

How to parse JSON string in Typescript

Is there a way to parse strings as JSON in Typescript. For ex. in JS, we can use JSON.parse(). Is there a similar function in Typescript?

I have a JSON object string as follows:

'{"name": "Bob", "error": false}'


Answer Source

Typescript is (a superset of) javascript, so you just use JSON.parse as you would in javascript:

let obj = JSON.parse(jsonString);

Only that in typescript you can have a type to the resulting object:

interface MyObj {
    myString: string;
    myNumber: number;

let obj: MyObj = JSON.parse('{ "myString": "string", "myNumber": 4 }');

(code in playground)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download