Author -  Sai gowtham

Reading a JSON file in Angular Quickly

In this tutorial, we are going to learn about how to read a (local) json file in angular with the help of an example.

Consider, we have this users.json file in our angular app.

users.json
[
  {
    "id": 23,
    "name": "Opera"
  },
  {
    "id": 24,
    "name": "Kevin"
  },
  {
    "id": 25,
    "name": "Lora"
  }
]

Reading the JSON file

  1. First, open the tsconfig.json file and add the following configuration to the compilerOptions object.
 "resolveJsonModule": true,
  1. Now, import the json file inside your angular component just like how we import other modules.
app.component.ts
import { Component } from '@angular/core';
import * as usersData from '../users.json';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  users: any = usersData;
  constructor() {
    console.log(this.users.default);  }
}

In the above code, we first imported everything * as usersData from the ../users.json file.

Then we accessed the usersData inside the AppComponent by assigning it to the users property.

In the constructor() method, we are logging the data into a browser console.

Note: Angular resolves the JSON file and converts it as a JavaScript object, where default property is holding the data.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY