Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse JSON object in Angular

I am trying to invoke "https://restcountries.com/v3.1/all" from a Angular application and when displaying the results in the HTML page, i am able to parse Name, flag , capital but when tried to display Currency its showing as [object object]

Here is the code i have written in html

<tbody>
    <tr *ngFor="let c of data">
      <td>{{ c.name.common }}</td>
      <td>{{ c.capital }}</td>
      <td>{{ c.flag }}</td>
       <td>{{ c.currencies }}</td>
    </tr>
  </tbody>

Here is the code i have written in Service

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 public getContrires():any {
    return this._httpClient.get('https://restcountries.com/v3.1/all');
  }

Result in HTML output coming as below

enter image description here

Thanks in advance.

I tried below in the html but still no luck

<td *ngFor="let cur of c.currencies">{{ curr.name }}</td>

>Solution :

You could iterate object properties using keyvalue pipe

Transforms Object or Map into an array of key value pairs.

enter image description here

<ul>
  <li *ngFor="let c of products">
    {{ c?.name?.official }} <br />
    <div *ngFor="let d of c?.currencies | keyvalue">
      {{ d.value['name'] }} - {{ d.value['symbol'] }}
    </div>
  </li>
</ul>

Please have a look at this stackblitz

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading