c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Angular

How to load data from json file in angular?

| | angular
In this tutorial I will show you how to bind data from json file in angular. I created a json file named data.json in local asset folder. Also imported data.json file in appcomponent and created an interface named "post" and declare properties and then assigned postjson to the interface object.
data.json file:
[
    {
      "id": 1,
      "title": "Nokia 8000 4G",
      "url": "https://www.chiptrolls.com/specs/Nokia-8000-4G/3056"
    },
    {
      "id": 2,
      "title": "Samsung Galaxy A20e",
      "url": "https://www.chiptrolls.com/specs/Samsung-Galaxy-A20e/3057"
    },
    {
      "id": 3,
      "title": "Realme C11",
      "url": "https://www.chiptrolls.com/specs/Realme-C11/3022"
    },
    {
      "id": 4,
      "title": "OnePlus Nord",
      "url": "https://www.chiptrolls.com/specs/OnePlus-Nord-5G/3015"
    },
    {
      "id": 5,
      "title": "Xiaomi POCO M2 Pr",
      "url": "https://www.chiptrolls.com/specs/Xiaomi-POCO-M2-Pro/3014"
    },
    {
      "id": 6,
      "title": "Oppo Reno 4 Pro",
      "url": "https://www.chiptrolls.com/specs/Oppo-Reno-4-Pro-5G/3013"
    },
    {
      "id": 7,
      "title": "Huawei Enjoy 20 Pro",
      "url": "https://www.chiptrolls.com/specs/Huawei-Enjoy-20-Pro/3012"
    },
    {
      "id": 8,
      "title": "Vivo X50 Pro",
      "url": "https://www.chiptrolls.com/specs/Vivo-X50-Pro/3011"
    },
    {
      "id": 9,
      "title": "LG Q61",
      "url": "https://www.chiptrolls.com/specs/LG-Q61/3008"
    },
    {
      "id": 10,
      "title": "Apple iPhone 12 Pro Max",
      "url": "https://www.chiptrolls.com/specs/Apple-iPhone-12-Pro-Max/3006"
    }]
blog.component.ts:
import { Component, OnInit } from '@angular/core';
import postJson from 'src/assets/data.json';
interface post {
  id: number;
  title: string;
  url: string;
}

@Component({
  selector: 'app-blog',
  templateUrl: './blog.component.html',
  styleUrls: ['./blog.component.css']
})
export class BlogComponent implements OnInit {

  posts: post[] = postJson;
  constructor() { }

  ngOnInit(): void {
  }

}
blog.component.html:
<div class="container mt-5">
    <h2>Posts</h2>
    <table class="table">
      <thead>
          <tr>
            <th>Id</th>
            <th>Title</th>
            <th>URL</th>
          </tr>
      </thead>
      <tbody>
        <tr *ngFor="let item of posts">
          <th scope="row">{{ item.id }}</th>
          <td>{{ item.title }}</td>
          <td>{{ item.url }}</td>
        </tr>
      </tbody>
    </table>
  </div>
If you got this following error "cannot find module consider using '--resolvejsonmodule' to import module with json extension angular" then click 
load data from json file