Skip to the solution
MediumFrontend6 min read

How do Import Attributes (with { type: "json" }) work and why did they replace import assertions?

656 views

The problem

Question presented to candidate: "Before import attributes existed, JSON.parse(fs.readFileSync(...)) or a bundler was the standard way to get JSON data into a module. What does import someData from './data.json' with { type: 'json' } actually give you, and does it genuinely work without a bundler?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Import Attributes (with { ... }) β€” syntax attached to an import statement (or dynamic import() call) declaring HOW the imported module must be interpreted β€” with { type: "json" } tells the engine to parse the target file as real JSON, not JavaScript.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: a genuine dynamic import(path, { with: { type: "json" } }) call, made against a REAL JSON file written to disk for this verification, correctly parsed the file and returned the real JSON content as the module's default export β€” with no bundler, no fs.readFileSync, no manual JSON.parse call.
  • πŸ“Œ Interview term: why they replaced the earlier "import assertions" β€” a precise answer names that an EARLIER, similar-looking proposal (import ... assert { type: "json" }) shipped first in some engines, but was later revised and renamed to with specifically because the TC39 committee decided the semantics needed to be OPPOSITE of what "assert" implies: an assertion, if wrong, was defined to still let the import proceed with a warning; the renamed with attribute is instead REQUIRED and genuinely changes how the module is interpreted β€” a real, meaningful semantic correction, not just a cosmetic keyword swap.
  • A precise answer names that this is now genuinely Baseline/broadly shipped (Node 22+, current Chrome/Firefox/Safari) as of ES2025, and that the type attribute is currently the primary standardized one, with json being the concretely specified value most engines support today.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering the prompt's own "does it work without a bundler" question with real proof is the strong signal.

Code / implementation expected: Yes β€” a real dynamic import with the with attribute against an actual JSON file written to disk is the clearest, most convincing demonstration that this is genuinely native, bundler-free behavior.

  • es2024
  • modules
  • json

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal, native JSON import with no manual JSON.parse β€” a real data: URL stands in for a genuine .json file so this runs anywhere

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 74 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How would you use ResizeObserver to react to an element's size changing, without polling getBoundingClientRect() on every frame?

Continue