With TypeScript 2.9.+ you can simply import JSON files with benefits like typesafety and intellisense by doing this:

import colorsJson from '../colors.json'; // This import style requires "esModuleInterop", see "side notes"
console.log(colorsJson.primaryBright);

Make sure to add these settings in the compilerOptions section of your tsconfig.json (documentation):

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

Side notes:

  • Typescript 2.9.0 has a bug with this JSON feature, it was fixed with 2.9.2
  • The esModuleInterop is only necessary for the default import of the colorsJson. If you leave it set to false then you have to import it with import * as colorsJson from '../colors.json'
Answer from kentor on Stack Overflow
🌐
Typescripttolua
typescripttolua.github.io › importing json modules
Importing JSON Modules | TypeScriptToLua
TypeScriptToLua supports importing JSON files into your TypeScript. This is done by translating the JSON file to Lua and simply including this Lua file as part of the transpiler output.
Top answer
1 of 16
900

With TypeScript 2.9.+ you can simply import JSON files with benefits like typesafety and intellisense by doing this:

import colorsJson from '../colors.json'; // This import style requires "esModuleInterop", see "side notes"
console.log(colorsJson.primaryBright);

Make sure to add these settings in the compilerOptions section of your tsconfig.json (documentation):

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

Side notes:

  • Typescript 2.9.0 has a bug with this JSON feature, it was fixed with 2.9.2
  • The esModuleInterop is only necessary for the default import of the colorsJson. If you leave it set to false then you have to import it with import * as colorsJson from '../colors.json'
2 of 16
222

The import form and the module declaration need to agree about the shape of the module, about what it exports.

When you write (a suboptimal practice for importing JSON since TypeScript 2.9 when targeting compatible module formatssee note)

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

You are stating that all modules that have a specifier ending in .json have a single export named default.

There are several ways you can correctly consume such a module including

import a from "a.json";
a.primaryMain

and

import * as a from "a.json";
a.default.primaryMain

and

import {default as a} from "a.json";
a.primaryMain

and

import a = require("a.json");
a.default.primaryMain

The first form is the best and the syntactic sugar it leverages is the very reason JavaScript has default exports.

However I mentioned the other forms to give you a hint about what's going wrong. Pay special attention to the last one. require gives you an object representing the module itself and not its exported bindings.

So why the error? Because you wrote

import a = require("a.json");
a.primaryMain

And yet there is no export named primaryMain declared by your "*.json".

All of this assumes that your module loader is providing the JSON as the default export as suggested by your original declaration.

Note: Since TypeScript 2.9, you can use the --resolveJsonModule compiler flag to have TypeScript analyze imported .json files and provide correct information regarding their shape obviating the need for a wildcard module declaration and validating the presence of the file. This is not supported for certain target module formats.

Discussions

New to TS, struggling with types of JSON module import.
So this is a static file you're working with? Any reason why you can't just turn it into .ts, save the JSON as a variable, then typecast it? Otherwise, have somesomething in the tune of export const obj = json as TypeIWant, then import obj instead of the raw JSON object. More on reddit.com
🌐 r/typescript
7
5
February 12, 2023
Need to read through a JSON file
Not a typescript question. const fs = require('fs'); const data = JSON.parse(fs.readFileSync('file.json')); More on reddit.com
🌐 r/typescript
2
0
May 8, 2020
Is there a way to import the type of a json file without importing the json file? (typescript, vite)
I'm pretty sure you can do import type object from "./file.json"; But then to reference the type, you'd still have to do typeof, e.g. declare const thing: typeof object; More on reddit.com
🌐 r/reactjs
10
0
November 7, 2023
How to force a type when importing a JSON file?
Use zod any time you have data coming from an external or untrusted source. More on reddit.com
🌐 r/typescript
13
7
June 11, 2022
🌐
JSON Formatter
jsonformatter.org › json-parser
JSON Parser Online to parse JSON
This JSON decode online helps to decode unreadable JSON. Sometimes it's mistyped or read as "JASON parser" or "JSON Decoder".
🌐
HackerNoon
hackernoon.com › import-json-into-typescript-8d465beded79
How to Import json into TypeScript | HackerNoon
November 28, 2016 - In ES6/ES2015, you can import json file in your code. For example,
🌐
Stack Abuse
stackabuse.com › reading-and-writing-json-with-typescript
Reading and Writing JSON with TypeScript
July 27, 2023 - Handling JSON in TypeScript can ... TypeScript's static typing system and with native JSON parsing and stringification methods. Proper error handling also plays an important role in creating error-free applications. Whether you're receiving data from an API or storing data in a file, understanding how to read and write ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-read-json-file-in-javascript
How to Read a JSON File in JavaScript – Reading JSON in JS
November 7, 2024 - This means we won't need to add the type of module, and also, we won't need to add the type of file. Neither method requires you to install a package or use a library as they are inbuilt. Choosing which method to use is totally up to you. But a quick tip that differentiates these methods is that the Fetch API reads a JSON file in JavaScript by sending an HTTP request, while the import statement does not require any HTTP request but rather works like every other import we make.
🌐
Max Gadget
maxgadget.dev › article › how-to-import-a-json-file-in-typescript-a-comprehensive-guide
How to Import a JSON file in Typescript: A Comprehensive Guide
May 14, 2023 - Yes, you can use typescript import JSON file with a dynamic file path by using the fs module in Node.js or the fetch() function in the browser. For instance, you can read and parse JSON files based on user input or other variables in your application.
Find elsewhere
🌐
Reddit
reddit.com › r/typescript › new to ts, struggling with types of json module import.
r/typescript on Reddit: New to TS, struggling with types of JSON module import.
February 12, 2023 -

Hi everyone, I'm new to TypeScript and currently I'm stuck with a problem for which I haven't found many resources on the internet, so I thought I could as you for some help.

Let me give you some context. I have a JSON file with the following content:

{
  "first": {
    "name": "FirstName",
    "age": 21
  },
  "second": {
    "name": "SecondName",
    "random": false
  }
}

My idea is that this files serves as some kind of hashmap, so I'm able to do something like this:

import data from 'test.json';

const access = (key: string) => {
  const value = data[key];

  const name = value?.name;
  const age = value?.age;
  const random = value?.random;
};

Ideally, this would mean that my JSON get's typed as something like:

type TypeIWant = {
  [key: string]: ValuesTypeIWant;
};

type ValuesTypeIWant = {
  name: string;
  age?: number;
  random?: boolean;
};

However, TypeScript gives me this type instead:

type TypeIGet = {
  first: {
    name: string;
    age: number;
  };
  second: {
    name: string;
    random: boolean;
  };
};

I undestand why TS it's giving me that type, but I want to use the values in the JSON file in dynamic routing, so I need to be able to do something like values[anyString], even if that returns me null, but I always get to an error similar to this one:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ first: { name: string; age: number; }; second: { name: string; random: boolean; }; }'.

The compiler doesn't even allow me to type cast it to TypeIWant. I've also tried creating a Map from those values, but still it doesn't work.

What can be done in this situation? Is there some way of telling the compiler that imported JSON data satisfied certain interface or type? Or is there a way to abstract the literal type that TS is generating from the JSON values to make it more generic, etc?

Considerations: I'm using the resolveJsonModule flag in my tsconfig and also, this is a simplified example, the full JSON has more than two keys, but the idea is the same, all keys have the same schema (if some keys are considered as optional).

🌐
Deno
docs.deno.com › examples › importing_json
Importing JSON
JSON files can be imported in JS and TS modules.
🌐
CodeSignal
codesignal.com › learn › courses › hierarchical-and-structured-data-formats-in-ts › lessons › parsing-json-files-in-typescript-using-nodejs
Parsing JSON Files in TypeScript Using Node.js
We'll use import to bring in the fs module and read from the file. TypeScript allows us to explicitly declare variable types, enhancing readability and reducing mistakes. Here, filePath is the path to the JSON file with a declared type of string.
🌐
SPGuides
spguides.com › typescript-import-json-file
How to Import JSON Files in TypeScript?
June 16, 2025 - Learn multiple methods to import JSON files in TypeScript with proper type safety. Step-by-step guide with real-world examples and troubleshooting tips.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › read-json-file-using-javascript
Read JSON File Using JavaScript - GeeksforGeeks
Use fetch("sample.json"), then parse the response with .json(). Handle the data or display it, using .catch() for errors. In a Node.js environment, require() is a simple way to read JSON files synchronously.
Published   January 17, 2026
🌐
Transform
transform.tools › json-to-typescript
JSON to TypeScript
An online playground to convert JSON to TypeScript
🌐
Futurestud.io
futurestud.io › tutorials › node-js-read-a-json-file
Node.js — Read a JSON File
the Fs.readJson method reads and parses the content of a JSON file and returns the resolved JavaScript value:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › JSON
Working with JSON - Learn web development | MDN
Many programming environments feature the ability to read (parse) and generate JSON. In JavaScript, the methods for parsing and generating JSON are provided by the JSON object. Note: Converting a string to a native object is called deserialization, while converting a native object to a string so it can be transmitted across the network is called serialization. A JSON string can be stored in its own file, which is basically just a text file with an extension of .json, and a MIME type of application/json.
🌐
npm
npmjs.com › package › json-server
json-server - npm
2 weeks ago - JSON Server automatically serves files from the ./public directory.
      » npm install json-server
    
Published   Mar 13, 2026
Version   1.0.0-beta.13
Author   typicode
🌐
Microsoft Developer Blogs
devblogs.microsoft.com › dev blogs › typescript › announcing typescript 6.0 rc
Announcing TypeScript 6.0 RC - TypeScript
1 week ago - If you have source files any level deeper than your tsconfig.json directory and were relying on TypeScript to infer a common root directory for source files, you’ll need to explicitly set rootDir:
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
TypeScript doesn’t assume the assignment of 1 to a field which previously had 0 is an error. Another way of saying this is that obj.counter must have the type number, not 0, because types are used to determine both reading and writing behavior.
🌐
Node.js
nodejs.org › api › fs.html
File system | Node.js v25.8.1 Documentation
Type: <number> The numeric file descriptor managed by the <FileHandle> object. ... position <integer> | <bigint> | <null> The location where to begin reading data from the file. If null or -1, data will be read from the current file position, and the position will be updated.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › Fetch_API › Using_Fetch
Using the Fetch API - Web APIs | MDN
Note that just like response bodies, request bodies are streams, and making the request reads the stream, so if a request contains a body, you can't make it twice: ... const request = new Request("https://example.org/post", { method: "POST", body: JSON.stringify({ username: "example" }), }); const response1 = await fetch(request); console.log(response1.status); // Will throw: "Body has already been consumed." const response2 = await fetch(request); console.log(response2.status);