Angular 6 - Load JSON from local

angular 6 read local json file
angular read file from path
how to read json file in angular 7 using http
read local json files in angular 8
ionic 4 read local json file
read json in angular service
how to read json object in angular
angular 7 get local json

I am trying to load a local JSONfile of two ways.

This is my json file:

{
  "imgsesion": "fa_closesesion.png",
  "texthome": "volver a la home",
  "logo": "fa_logo.png",
  "menu": {
    "background": "orange",
    "link1": "ESCRITOR",
    "link2": "MÚSICO",
    "link3": "AYUDA ADMIN",
    "submenu": {
      "link1": {
        "text1": "novelas",
        "text2": "obras de teatro"
      },
      "link2": {
        "text1": "compositor",
        "text2": "intérprete"
      }
    }
  }
}
  • Way 1: Using Http

This is my service file (general.service.ts)

  getContentJSON() {
    return this.http.get('assets/json/general.json')
    .map(response => response.json());
  }

This way working ok, but shows the next error in the web browser console:

ERROR TypeError: Cannot read property 'menu' of undefined
  • Way 2: Using HttpClient

This is my service file (general.service.ts)

  getContentJSON() {
    return this.httpClient.get("assets/json/general.json");
  }

It does not work because I can not find the general.json file, it goes through the control of the error and it gives me an error 404

This is the component file (app.component.ts)

export class AppComponent implements OnInit {
  contentGeneral: any;

ngOnInit() {
this.getContentJSON();
}

  getContentJSON() {
    this.generalService.getContentJSON().subscribe(data => {
      this.contentGeneral = data;
    }, // Bind to view
    err => {
      // Log errors if any
      console.log('error: ', err);
    });
  }

}

This is the template file (app.component.html):

<a href="#" routerLink="/home" class="linkHome">{{contentGeneral.texthome}}</a>

<div class="submenu" *ngIf="linkWrite.isActive || isSubMenuWriter">
    <span class="d-block text-right small">{{contentGeneral.menu.submenu.link1.text1}}</span>
    <span class="d-block text-right small">{{contentGeneral.menu.submenu.link1.text2}}</span>
</div>

This is my actual error:

In app.component.ts, I add the import:

import * as data_json from './assets/json/general.json';

But when I launch ng serve it gives me the following error:

How I could resolve it?


The simplest solution:

import "myJSON" from "./myJson"

Important update!

I found, that this method stops working in newest Angular versions, because of this error:

ERROR in src/app/app.weather.service.ts(2,25): error TS2732: Cannot find module './data.json'. Consider using '--resolveJsonModule' to import module with '.json' extension

To make it works, go to the tsconfig.json and add this two, inside

compilerOptions( tsconfig.json ) :

"resolveJsonModule": true,
"esModuleInterop": true,

After change, re-run ng serve.

If you only use the first option, you can get an error like this:

ERROR in src/app/app.weather.service.ts(2,8): error TS1192: Module '"....app/data/data.json"' has no default export.

(I found this very good answer here (https://www.angularjswiki.com/angular/how-to-read-local-json-files-in-angular/))

Angular 6 - Load JSON from local, The simplest solution: import "myJSON" from "./myJson". Important update! I found, that this method stops working in newest Angular versions,  We can import or read local JSON files in Angular,several different ways depending upon the Angular version we are using. In this post I am listing out different methods with examples on how to read JSON files in Angular. Angular framework supports TypeScript 2.9 from 6.1 version onwards.In typescript 2.9 with the help of resolveJsonModule we


By this way...

import "file" from "./file.json" 

I got red line and got error like module not found by angular.

After some RND I got another way which works for me. Hope it may help someone.

var data = require('src/file.json');
console.log("Json data : ", JSON.stringify(data));

Angular 5 Service to read local .json file, First You have to inject HttpClient and Not HttpClientModule , second thing you have to remove .map((res:any) => res.json()) you won't need it any more  Step 5 - Displaying the JSON Data. Finally, open the src/app/app.component.html file and add the following code: [% raw %} ```html ``` [% endraw %} Conclusion. In this quick example, we've seen how to use the import statement in TypeScript 2.9+ to import and load local JSON files in our Angular 8 apps.


See this article:

import data  from './data.json';
export class AppComponent  {
    json:any = data;
}

3 Methods for Reading Local JSON Files in Angular 7/8, In this tutorial, we'll present you 3 ways to read local JSON files in your Angular 8 application with examples. There are several ways to read local JSON files in Angular. I’m going to show you the easiest way to read local JSON files which works for both online and Offline Angular app. We’ll then add Material Design Bootstrap for styling and data display and deploy the app live using Netlify. Assumptions:


For the Angular 2, Angular 4 and Angular 5, you can use following method to load your local .json files to the component.

const data = require('../../data.json');

export class AppComponent  {
    json:any = data;
}

To use require with in your component, you needed to install @types/node by running

npm install @types/node --save-dev

Do the following configuration change under tsconfig.app.json > compilerOptions

"compilerOptions": {
  "types": ["node"],
  ...
},

After Angular 6, you can use direct imports form file system as follows.

import data  from '../../data.json';

export class AppComponent  {
    json:any = data;
}

Do the following configuration change under tsconfig.app.json > compilerOptions

"compilerOptions": {
  ...
  "resolveJsonModule": true,
  "allowSyntheticDefaultImports": true,
  "esModuleInterop": true,
  ...
},

Import & Read Local JSON Files in Angular 7/8 and TypeScript 2.9+ , In this tutorial, we'll see by example how to import and read local JSON files in Angular 8 applications and TypeScript 2.9+. In this tutorial, we’ll learn how to import and read local JSON files in Angular 8 applications and TypeScript 2.9 or above. There are various ways to read local JSON files but in this example we’ll see how to use the import statement to import a local JSON file just like any TypeScript module which is a supported feature in TypeScript 2.9+.


This happens because you are requesting a JSON file asynchronously, you can handle with safe navigation operator or using ngIf,

<div class="submenu" *ngIf="linkWrite.isActive || isSubMenuWriter">
                  <span class="d-block text-right small">{{contentGeneral?.menu?.submenu?.link1?.text1}}</span>
                  <span class="d-block text-right small">{{contentGeneral?.menu?.submenu?.link1?.text2}}</span>
                </div>

or simply import the JSON file in your component and assign sampleJSON.

import "sampleJSON" from "./sampleJSON"

How to Read Local JSON Files In Angular 7|8|9, Let me show you the easiest way to read local JSON files in Offline Angular app. Let's create a JSON file, we name it data.json you can name it  In this tutorial, I am going to teach you how to read local JSON files in Angular 7|8|9 application. There are various methods available to read local JSON files in Angular. Let me show you the easiest way to read local JSON files in Offline Angular app. Let’s create a JSON file, we name it data.json you can name it whatever you want!


How to Read Local JSON Files & Deploy an Angular App, I have been playing soccer since I was 6; I played in my middle school team, high school, college, semi-pro, and currently playing young adult  You can use the HttpClient service in Angular 4 to read and extract data from an external JSON file. Using the Get() method of HttpClient class, you can easily open and read data from a JSON file. Here in this post, I am sharing a simple example on how to read JSON data from a file and convert the data to an HTML table.


How to Read Local JSON file in Angular, How to Read Local JSON file in Angular. September 28, 2019 by Suraj Roy. Today, I am going to create a sample application to show how to use local JSON file  In this tutorial, I am going to teach you how to read local JSON files in Angular 2 | 7 | 8 application. There are various methods available to read local JSON files in Angular. Let me show you the easiest way to read local JSON files in Offline Angular app. Let’s create a JSON file, we name it data.json you can name it whatever you want


Import & Read Local JSON Files in Angular 7/8, In this tutorial, we'll learn how to import and read local JSON files in Angular Next, let's create a local JSON file in our Angular project's folder. Angular web apps · Beginning Angular with Typescript (updated to Angular 6)  In angular 5 there is not built in directive or module to import JSON files in any component easily. So, how do we read a local JSON file located in the angular project itself? Well, the most popular approach to tackle this is to use AJAX.