how to show key and value of json on table in angular 2

1.1k views Asked by At
confirmation_token:  null
created_by: null
dob: "1995-04-09T18:30:00.000Z"
email : "[email protected]"
firstName : "yash"
fk_status_id : 2
id : 258
is_blocked : false

This is my json file that I am getting in response in angular response object. I want to show both key and value pair on HTML I am not able to get how to do the same. Please help me.

2

There are 2 answers

0
swaroop pallapothu On

Capture your response in an variable. And create dataset based on your response User tableData to iterate your rows in an table.

var response = {
    confirmation_token:  null
    created_by: null
    dob: "1995-04-09T18:30:00.000Z"
    email : "[email protected]"
    firstName : "yash"
    fk_status_id : 2
    id : 258
    is_blocked : false
};

var tableData = [];

for ( var key in response ) {
    var data = {
    };
    data[key] = response[key];
    tableData.push(data);
}
0
pawan On

if you want to iterate on html u can do like

<div *ngFor="let (val,key) of youObj">
   <span>{{val}}</span>
   <span>{{key}}</span>