Home > Back-end >  Angular - get data from json file
Angular - get data from json file

Time:11-16

A have a something.json file

{
"key":"value",
"key2":"value2"
}

And angular component. Now I try to import and use JSON file

import * as myJson from '../assets/something.json';
...
myJson['key']

Problem is that I am getting error

 Error: Should not import the named export 'key' (imported as 'myJson') from default-exporting module (only default export is available soon)

But I have no idea how to solve it. I will be thankful for help.

CodePudding user response:

You need "resolveJsonModule": true, in your tsconfig, and an import like this:

import myData from './data.json';

And then you have acces to myData.whatever

Heres a working example: https://stackblitz.com/edit/angular-ivy-zgfzqk?file=tsconfig.json

CodePudding user response:

I just needed to put myJson into new variable so.

myJsonValuesHolder = myJson
  • Related