close

How to parse JSON string in Typescript

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to parse JSON string in Typescript in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to parse JSON string in Typescript ?

  1. How to parse JSON string in Typescript ?

    to parse JSON string in Typescript Yes it's a little tricky in TypeScript but you can do the below example like this

    let decodeData = JSON.parse(`${jsonResponse}`);

  2. How to parse JSON string in Typescript ?

    to parse JSON string in Typescript Yes it's a little tricky in TypeScript but you can do the below example like this

    let decodeData = JSON.parse(`${jsonResponse}`);

Method 1

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 }');
console.log(obj.myString);
console.log(obj.myNumber);

Method 2

Yes it’s a little tricky in TypeScript but you can do the below example like this

let decodeData  =  JSON.parse(`${jsonResponse}`);

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment