Beginner
cliweb
Importing JSON
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
!--frsh-copybutton:1-->
import file from "./version.json" assert { type: "json" };
console.log(file.version);
Dynamic imports are also supported.
./main.ts
!--frsh-copybutton:2-->
const module = await import("./version.json", {
assert: { type: "json" },
});
console.log(module.default.version);
./version.json
!--frsh-copybutton:4-->
{
"version": "1.0.0"
}
Run this example locally using the Deno CLI:
deno run --allow-net https://byexample-wwv03xf36j0g.deno.dev/importing-json/main.ts