Reading a JSON file in Angular Quickly
In this post we will give you information about Reading a JSON file in Angular Quickly. Hear we will give you detail about Reading a JSON file in Angular QuicklyAnd how to use it also give you demo for it if it is necessary.
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.
[ { "id": 23, "name": "Opera" }, { "id": 24, "name": "Kevin" }, { "id": 25, "name": "Lora" }]
Reading the JSON file
- First, open the tsconfig.json file and add the following configuration to the compilerOptions object.
"resolveJsonModule": true,
- Now, import the json file inside your angular component just like how we import other modules.
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.
Hope this code and post will helped you for implement Reading a JSON file in Angular Quickly. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs