I get the following JSON represantion from a web service
{
"STAT": {
"Total": 216,
"Average": 2.9722222222222223
},
"PPRP": {
"Total": 31494,
"Average": 19.884390677589384
}
}
Within my component I have the following code:
rawOverview: any
ngOnInit() {
this.service.getPlcOverview().subscribe(
data => {
this.rawOverview = JSON.parse(JSON.stringify(data))
},
err => console.error(err),
() => console.log('done loading foods')
);
}
How can I access PPRP and STAT with template syntax?
I tried the following:
<table class="table table-dark">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Tels with acks</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">PPRP</th>
<td>{{rawOverview.PPRP?.Average}}</td>
</tr>
</tbody>
</table>
but I get an error in the console.
ERROR TypeError: Cannot read property 'PPRP' of undefined
Is it possible to access a JSON object in this way? Or do I have to create known objects which can access the values?
{{rawOverview?.PPRP?.Average}}. Intially,rawOverviewwill be undefined, so adding the null safe operator will prevent the errorJSON.parse(JSON.stringify(data))- why?