{"id":22722,"date":"2025-10-22T12:42:49","date_gmt":"2025-10-22T12:42:49","guid":{"rendered":"http:\/\/localhost\/?p=22722"},"modified":"2025-10-22T12:42:49","modified_gmt":"2025-10-22T12:42:49","slug":"importing-vs-fetching-json","status":"publish","type":"post","link":"https:\/\/zero.redgem.net\/?p=22722","title":{"rendered":"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F"},"content":{"rendered":"<p>{&#8220;lastseen&#8221;:&#8221;2025-10-22T16:39:39&#8243;,&#8221;description&#8221;:&#8221;This year, JSON module imports became baseline &#8216;newly available&#8217;, meaning they&#8217;re implemented across browser engines.\\n    \\n    \\n    import data from &#8216;.\/data.json&#8217; with { type: &#8216;json&#8217; };\\n    \\n    \/\/ And\u2026\\n    \\n    const { default: data } = await import(&#8216;.\/data.json&#8217;, {\\n      with: { type: &#8216;json&#8217; },\\n    });\\n\\nI&#8217;m glad JavaScript has this feature, but I can&#8217;t see myself using it in a browser environment, other than small demos. It comes down to the behaviour differences with this:\\n    \\n    \\n    const response = await fetch(&#8216;.\/data.json&#8217;);\\n    const data = await response.json();\\n\\nHere&#8217;s why\u2026\\n\\n## Error handling\\n\\nWith a static import:\\n    \\n    \\n    import data from &#8216;.\/data.json&#8217; with { type: &#8216;json&#8217; };\\n\\nIf the above fails, it takes the whole module graph down with it. Because of this, I&#8217;d never use this pattern with some third-party JSON, as I&#8217;d want to be able to provide a fallback if the third-party service fails.\\n\\nBut `import()` allows exactly that:\\n    \\n    \\n    try {\\n      const { default: data } = await import(url, {\\n        with: { type: &#8216;json&#8217; },\\n      });\\n    } catch (error) {\\n      \/\/ Fallback logic\\n    }\\n\\nThis is pretty good. Although the `fetch()` alternative:\\n    \\n    \\n    try {\\n      const response = await fetch(&#8216;.\/data.json&#8217;);\\n      const data = await response.json();\\n    } catch (error) {\\n      \/\/ Fallback logic\\n    }\\n\\n\u2026allows much more introspection in event of a failure. There&#8217;s `response.status`, or you can use `response.text()`, meaning you still have the source if JSON parsing fails. Maybe that doesn&#8217;t matter in all cases.\\n\\nI think the bigger issue is\u2026\\n\\n## Caching and garbage collection\\n\\nWhen you import a module (be it JS, WASM, CSS, or JSON), it&#8217;s cached for the lifetime of the environment (e.g. a page or worker), even if the result is a network or parsing failure. All imports for a given specifier \\u0026 type return the same module.\\n\\nThis is generally a good thing, as it means your JS module returns the same objects every time, and state can be shared across all importers. But if you&#8217;re doing something like:\\n    \\n    \\n    const { default: results } = await import(&#8216;\/api\/search?q=whatever&#8217;, {\\n      with: { type: &#8216;json&#8217; },\\n    });\\n\\n\u2026then you have a memory leak, because each set of search results will live in the module graph for the life of the page. That isn&#8217;t the case with `fetch()`, where returned objects can be garbage collected once they&#8217;re out of reference.\\n\\nThe same applies to cases like:\\n    \\n    \\n    let { default: largeData } = await import(&#8216;\/large-data.json&#8217;, {\\n      with: { type: &#8216;json&#8217; },\\n    });\\n    \\n    const someSmallPart = largeData.slice(0, 10);\\n    largeData = null;\\n\\nIf the above used `fetch()`, then the data other than `someSmallPart` could be garbage collected. But with `import()`, the whole `largeData` object remains in memory for the life of the page.\\n\\n## When should JSON modules be used?\\n\\nIt makes sense to use JSON module imports for local static JSON resources where you need all\/most of the data within. Particularly since bundlers can understand JSON imports, and bundle the object with other modules. That isn&#8217;t possible with `fetch()`, unless you use some pretty hacky plugins.\\n\\nIn server code, I might import `package.json` to get the version number. However, I wouldn&#8217;t do this with frontend code, as it&#8217;s wasteful to bundle all of `package.json` just to get a single value \u2013 bundlers don&#8217;t perform tree-shaking of individual object keys.\\n\\n**Update:** Jed points out that esbuild has an option at allows you to import JSON as if each top level key is individually exported, and in this case it will tree-shake. You could make a fairly trivial plugin to make the same work for Rollup\/Vite. It still requires you to use the right kind of import, though.\\n\\nGenerally, I&#8217;d write a Vite\/Rollup plugin to extract just the data I needed at build time (with Vite, the define option is handy).\\n\\nI&#8217;m glad this feature exists, but it should be used with care, and not as a blanket replacement for `fetch()`ing JSON.&#8221;,&#8221;published&#8221;:&#8221;2025-10-22T01:00:00&#8243;,&#8221;modified&#8221;:&#8221;2025-10-22T01:00:00&#8243;,&#8221;type&#8221;:&#8221;jakearchibald&#8221;,&#8221;title&#8221;:&#8221;Importing vs fetching JSON&#8221;,&#8221;source&#8221;:&#8221;&#8221;,&#8221;references&#8221;:&#8221;&#8221;,&#8221;id&#8221;:&#8221;JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F&#8221;,&#8221;bulletinFamily&#8221;:&#8221;blog&#8221;,&#8221;cwe&#8221;:null,&#8221;cvelist&#8221;:[],&#8221;sourceData&#8221;:&#8221;&#8221;,&#8221;sourceHref&#8221;:&#8221;&#8221;,&#8221;cvss&#8221;:{&#8220;score&#8221;:0,&#8221;severity&#8221;:&#8221;NONE&#8221;,&#8221;vector&#8221;:&#8221;NONE&#8221;,&#8221;version&#8221;:&#8221;NONE&#8221;},&#8221;cvss2&#8243;:{},&#8221;cvss3&#8243;:{&#8220;version&#8221;:&#8221;&#8221;,&#8221;vectorString&#8221;:&#8221;&#8221;,&#8221;baseScore&#8221;:0,&#8221;baseSeverity&#8221;:&#8221;&#8221;,&#8221;attackVector&#8221;:&#8221;&#8221;,&#8221;attackComplexity&#8221;:&#8221;&#8221;,&#8221;privilegesRequired&#8221;:&#8221;&#8221;,&#8221;userInteraction&#8221;:&#8221;&#8221;,&#8221;scope&#8221;:&#8221;&#8221;,&#8221;confidentialityImpact&#8221;:&#8221;&#8221;,&#8221;integrityImpact&#8221;:&#8221;&#8221;,&#8221;availabilityImpact&#8221;:&#8221;&#8221;,&#8221;cvssV3&#8243;:{&#8220;version&#8221;:&#8221;&#8221;,&#8221;vectorString&#8221;:&#8221;&#8221;,&#8221;baseScore&#8221;:0,&#8221;baseSeverity&#8221;:&#8221;&#8221;,&#8221;attackVector&#8221;:&#8221;&#8221;,&#8221;attackComplexity&#8221;:&#8221;&#8221;,&#8221;privilegesRequired&#8221;:&#8221;&#8221;,&#8221;userInteraction&#8221;:&#8221;&#8221;,&#8221;scope&#8221;:&#8221;&#8221;,&#8221;confidentialityImpact&#8221;:&#8221;&#8221;,&#8221;integrityImpact&#8221;:&#8221;&#8221;,&#8221;availabilityImpact&#8221;:&#8221;&#8221;}},&#8221;href&#8221;:&#8221;https:\/\/jakearchibald.com\/2025\/importing-vs-fetching-json\/&#8221;,&#8221;category_name&#8221;:&#8221;News&#8221;,&#8221;post_link&#8221;:&#8221;&#8221;,&#8221;product&#8221;:&#8221;&#8221;,&#8221;version&#8221;:&#8221;&#8221;,&#8221;vendor&#8221;:&#8221;&#8221;,&#8221;ai_description&#8221;:&#8221;&#8221;,&#8221;ai_severity&#8221;:&#8221;&#8221;,&#8221;ai_vendor&#8221;:&#8221;&#8221;,&#8221;ai_product&#8221;:&#8221;&#8221;,&#8221;ai_version&#8221;:&#8221;&#8221;,&#8221;ai_score&#8221;:0}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>{&#8220;lastseen&#8221;:&#8221;2025-10-22T16:39:39&#8243;,&#8221;description&#8221;:&#8221;This year, JSON module imports became baseline &#8216;newly available&#8217;, meaning they&#8217;re implemented across browser engines.\\n \\n \\n import data from &#8216;.\/data.json&#8217; with { type: &#8216;json&#8217;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[6,8,12,161,13,33,7,11,5],"class_list":["post-22722","post","type-post","status-publish","format-standard","hentry","category-category_news","tag-cve","tag-cvss","tag-exploit","tag-jakearchibald","tag-news","tag-none","tag-security","tag-tapic","tag-vulnerability"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F - zero redgem<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zero.redgem.net\/?p=22722\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F - zero redgem\" \/>\n<meta property=\"og:description\" content=\"{&#8220;lastseen&#8221;:&#8221;2025-10-22T16:39:39&#8243;,&#8221;description&#8221;:&#8221;This year, JSON module imports became baseline &#8216;newly available&#8217;, meaning they&#8217;re implemented across browser engines.n n n import data from &#8216;.\/data.json&#8217; with { type: &#8216;json&#8217;...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zero.redgem.net\/?p=22722\" \/>\n<meta property=\"og:site_name\" content=\"zero redgem\" \/>\n<meta property=\"article:published_time\" content=\"2025-10-22T12:42:49+00:00\" \/>\n<meta name=\"author\" content=\"invoker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"invoker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/?p=22722#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/?p=22722\"},\"author\":{\"name\":\"invoker\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#\\\/schema\\\/person\\\/fbfeae8dfad117ac08a7621bee1a1dca\"},\"headline\":\"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F\",\"datePublished\":\"2025-10-22T12:42:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/?p=22722\"},\"wordCount\":809,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#organization\"},\"keywords\":[\"CVE\",\"CVSS\",\"exploit\",\"jakearchibald\",\"news\",\"NONE\",\"Security\",\"tapic\",\"Vulnerability\"],\"articleSection\":[\"category_news\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zero.redgem.net\\\/?p=22722#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/?p=22722\",\"url\":\"https:\\\/\\\/zero.redgem.net\\\/?p=22722\",\"name\":\"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F - zero redgem\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#website\"},\"datePublished\":\"2025-10-22T12:42:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/?p=22722#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zero.redgem.net\\\/?p=22722\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/?p=22722#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zero.redgem.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#website\",\"url\":\"https:\\\/\\\/zero.redgem.net\\\/\",\"name\":\"zero redgem\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zero.redgem.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#organization\",\"name\":\"zero redgem\",\"url\":\"https:\\\/\\\/zero.redgem.net\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"\",\"contentUrl\":\"\",\"width\":191,\"height\":188,\"caption\":\"zero redgem\"},\"image\":{\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zero.redgem.net\\\/#\\\/schema\\\/person\\\/fbfeae8dfad117ac08a7621bee1a1dca\",\"name\":\"invoker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f17c01d7338e6932bcde121cf83569393df3374625d25afd62677cfb528f2e3e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f17c01d7338e6932bcde121cf83569393df3374625d25afd62677cfb528f2e3e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f17c01d7338e6932bcde121cf83569393df3374625d25afd62677cfb528f2e3e?s=96&d=mm&r=g\",\"caption\":\"invoker\"},\"sameAs\":[\"https:\\\/\\\/zero.redgem.net\"],\"url\":\"https:\\\/\\\/zero.redgem.net\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F - zero redgem","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/zero.redgem.net\/?p=22722","og_locale":"en_US","og_type":"article","og_title":"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F - zero redgem","og_description":"{&#8220;lastseen&#8221;:&#8221;2025-10-22T16:39:39&#8243;,&#8221;description&#8221;:&#8221;This year, JSON module imports became baseline &#8216;newly available&#8217;, meaning they&#8217;re implemented across browser engines.n n n import data from &#8216;.\/data.json&#8217; with { type: &#8216;json&#8217;...","og_url":"https:\/\/zero.redgem.net\/?p=22722","og_site_name":"zero redgem","article_published_time":"2025-10-22T12:42:49+00:00","author":"invoker","twitter_card":"summary_large_image","twitter_misc":{"Written by":"invoker","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zero.redgem.net\/?p=22722#article","isPartOf":{"@id":"https:\/\/zero.redgem.net\/?p=22722"},"author":{"name":"invoker","@id":"https:\/\/zero.redgem.net\/#\/schema\/person\/fbfeae8dfad117ac08a7621bee1a1dca"},"headline":"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F","datePublished":"2025-10-22T12:42:49+00:00","mainEntityOfPage":{"@id":"https:\/\/zero.redgem.net\/?p=22722"},"wordCount":809,"commentCount":0,"publisher":{"@id":"https:\/\/zero.redgem.net\/#organization"},"keywords":["CVE","CVSS","exploit","jakearchibald","news","NONE","Security","tapic","Vulnerability"],"articleSection":["category_news"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zero.redgem.net\/?p=22722#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zero.redgem.net\/?p=22722","url":"https:\/\/zero.redgem.net\/?p=22722","name":"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F - zero redgem","isPartOf":{"@id":"https:\/\/zero.redgem.net\/#website"},"datePublished":"2025-10-22T12:42:49+00:00","breadcrumb":{"@id":"https:\/\/zero.redgem.net\/?p=22722#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zero.redgem.net\/?p=22722"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zero.redgem.net\/?p=22722#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zero.redgem.net\/"},{"@type":"ListItem","position":2,"name":"Importing vs fetching JSON_JAKEARCHIBALD:A496D7960199E35FDA15EAD6611BA94F"}]},{"@type":"WebSite","@id":"https:\/\/zero.redgem.net\/#website","url":"https:\/\/zero.redgem.net\/","name":"zero redgem","description":"","publisher":{"@id":"https:\/\/zero.redgem.net\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zero.redgem.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/zero.redgem.net\/#organization","name":"zero redgem","url":"https:\/\/zero.redgem.net\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zero.redgem.net\/#\/schema\/logo\/image\/","url":"","contentUrl":"","width":191,"height":188,"caption":"zero redgem"},"image":{"@id":"https:\/\/zero.redgem.net\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/zero.redgem.net\/#\/schema\/person\/fbfeae8dfad117ac08a7621bee1a1dca","name":"invoker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f17c01d7338e6932bcde121cf83569393df3374625d25afd62677cfb528f2e3e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f17c01d7338e6932bcde121cf83569393df3374625d25afd62677cfb528f2e3e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f17c01d7338e6932bcde121cf83569393df3374625d25afd62677cfb528f2e3e?s=96&d=mm&r=g","caption":"invoker"},"sameAs":["https:\/\/zero.redgem.net"],"url":"https:\/\/zero.redgem.net\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/zero.redgem.net\/index.php?rest_route=\/wp\/v2\/posts\/22722","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zero.redgem.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zero.redgem.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zero.redgem.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zero.redgem.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22722"}],"version-history":[{"count":0,"href":"https:\/\/zero.redgem.net\/index.php?rest_route=\/wp\/v2\/posts\/22722\/revisions"}],"wp:attachment":[{"href":"https:\/\/zero.redgem.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22722"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zero.redgem.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22722"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zero.redgem.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22722"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}