Beginner

cliweb

Importing JSON

Edit

JSON files can be imported in JS and TS files using the `import` keyword. This makes including static data in a library much easier.

JSON files can be imported in JS and TS modules. When doing so, you need to specify the "json" import assertion type.
./main.ts
import file from "./version.json" with { type: "json" };
console.log(file.version);
Dynamic imports are also supported.
./main.ts
const module = await import("./version.json", {
  with: { type: "json" },
});
console.log(module.default.version);
./version.json
{
  "version": "1.0.0"
}

Run this example locally using the Deno CLI:

deno run https://raw.githubusercontent.com/denoland/deno-docs/main/by-example/importing-json.ts/main