How to import JSON File into a TypeScript file?

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 content

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

and 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:

  1. Add resolveJsonModule support with this line in your tsconfig.json file:
"compilerOptions": {
    ...
    "resolveJsonModule": true
  }

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.

Leave a Comment