David
David

Reputation: 4508

parse Json response using angular

In my ts file I have function which sends some data and then receives json response

const fd = new FormData();  
this.service.uploadImage(fd).subscribe(
    (res:any)=>
    {
      console.log(res) ;

In this way I get the whole json console.logged.

{item1: "abcdefg", item2: "xyz"}

But I want to access each component of json seperately. How can I do that ?

Upvotes: 0

Views: 50

Answers (2)

Taimoor Tahir
Taimoor Tahir

Reputation: 440

Actually it depends on the format of JSON response you are getting, but in this particular case, you can do this by . operator, like

res.item1
res.item2

Upvotes: 1

CharybdeBE
CharybdeBE

Reputation: 1843

your console log look like an object not a json, so i don't think you need to parse, you can just access res.item1 to get value one

If it does not work and indeed is a json first parse your string via const parsedRes = JSON.parse(res)

Upvotes: 0

Related Questions