I am building a map application using Angular Maps and want to import a JSON file as a list of markers defining locations. I'm hoping to use this JSON file as marker[] array inside the app.component.ts . Instead of defining a hardcoded array of markers inside the TypeScript file.
What is the best process of importing this JSON file for use in my project? Any direction greatly appreciated!
For Angular 7+,
1) add a file "typings.d.ts" to the project's root folder (e.g., src/typings.d.ts):
2) import and access JSON data either:
or:
Thanks for the input guys, I was able to find the fix, I added and defined the json on top of the app.component.ts file:
This ultimately produced the markers and is a simple line of code.
Aonepathan's one-liner was working for me until a recent typescript update.
I found Jecelyn Yeen's post which suggests posting this snippet into your TS Definition file
add file
typings.d.ts
to the project's root folder with below contentand then import your data like this:
import * as data from './example.json';
update July 2019:
Typescript 2.9 (docs) introduced a better, smarter solution. Steps:
resolveJsonModule
support with this line in yourtsconfig.json
file:the import statement can now assumes a default export:
import data from './example.json';
and intellisense will now check the json file to see whether you can use Array etc. methods. pretty cool.
In angular7, I simply used
My routes.json file looks like this
You access json items using