17

In Angular2 you could have a folder /data/ and a json file there and you could access it at localhost:4200/data/something.json.

This is no longer possible in Angular4.

Any ideea how to get it to work?

Dan Neciu
  • 208
  • 1
  • 2
  • 9

7 Answers7

14

you can use this code

@Injectable()
export class AppServices{

    constructor(private http: Http) {
         var obj;
         this.getJSON().subscribe(data => obj=data, error => console.log(error));
    }

    public getJSON(): Observable<any> {
         return this.http.get("./file.json")
                         .map((res:any) => res.json())
                         .catch((error:any) => console.log(error));

     }
}

here file.json is your local json file.

see here also

also see the changlog of angular-cli for path

Community
  • 1
  • 1
Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
  • This is what i used to do in Angular2. In Angular4 the route ./file.json no longer works. – Dan Neciu Apr 07 '17 at 10:42
  • @DanNeciu also see my Updated answer – Pardeep Jain Apr 07 '17 at 10:52
  • 1
    that's why frameworks suck, all this just to open file, it may be easier to do it with actual code interfacing with the file system itself – aero Sep 19 '18 at 22:23
  • @Preadeep. import * as data from './example.json'; or subscribing .json from local assets which one is preferred and why to subscribe if we can import directly? – Mahi Jan 18 '19 at 18:08
  • @Mahi IMO Both are different ways to access local JSON data in your project, you can choose either. Offcourse If we can access directly without HTTP call simple import would be a more good idea. and to get data from observable you need to subscribe I guess but yes you can omit `.map` from here. – Pardeep Jain Jan 19 '19 at 14:39
8

Ofcourse its possible. Let's assume here is your json file

enter image description here


And here is your code to call the json

import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map'

@Injectable()
export class YourService {

   constructor(private http: Http) { }

   getAdvantageData(){
      let apiUrl = './assets/data/api/advantage.json';
      return this.http.get(apiUrl)
      .map( (response: Response) => {
         const data = response.json();
         return data;
      });
   }  
}
Tienus McVinger
  • 467
  • 9
  • 24
Vivek Shukla
  • 711
  • 9
  • 18
7

I was facing the same issue, where my Observable Angular service was located at inside 'src/app/' folder and it was trying to load a local JSON file. I tried to put the JSON file inside the same app folder, inside a new 'api' folder, used various kinds of relatives/absolute routes, but it didn't help and I got 404 error. The moment I put it inside 'assets' folder, it worked. I guess there is more to it?

Maybe this link helps:

COMPONENT-RELATIVE PATHS IN ANGULAR

halfer
  • 19,824
  • 17
  • 99
  • 186
Deepak Pathak
  • 626
  • 1
  • 9
  • 19
  • 1
    I think webpack, the module bundler, bundles by default everything from assets folder. – Deepak Pathak Oct 29 '17 at 17:15
  • It's because of everything specified in the `assets` array in `angular-cli.json` gets copied and served, thus being available in the runtime. See https://github.com/angular/angular-cli/wiki/stories-asset-configuration – Nerman Mar 08 '18 at 22:36
7

You can use "require" too;

let test = require('./test.json');
yazantahhan
  • 2,950
  • 1
  • 13
  • 16
3

Based on this post, here is complete answer for Angular 6+:

From angular-cli doc, json can be considered as assets and accessed from standard import without use of ajax request.

Let's suppose you add your json files into "your-json-dir" directory:

  1. add "your-json-dir" into angular.json file (:

    "assets": [ "src/assets", "src/your-json-dir" ]

  2. allow import of json modules into typings.d.ts file to prevent from typescript errors:

    declare module "*.json" { const value: any; export default value; }

  3. in your controller/service/anything else file, simply import the file by using this relative path:

    import * as myJson from 'your-json-dir/your-json-file.json';

Benjamin Caure
  • 2,090
  • 20
  • 27
0

I figured it out.

In Angular2 I had the folder under the src folder. In Angular4 you have to have it in the root folder.

Example:

Angular2:

root / src / data / file.json

Angular4:

root / data / file.json

Dan Neciu
  • 208
  • 1
  • 2
  • 9
  • You can place file wherever you want, just as you use the whole path, not relative paths in your request :) – AT82 Apr 07 '17 at 11:06
0

You could add your folder location under Assets tag in angular.json

"assets": [
          "src/favicon.ico",
          "src/assets",
          "src/api"
        ],
deathrace
  • 908
  • 4
  • 22
  • 48