How can I access a JSON file in ECMAScript 6?
The following doesn't work:
import config from '../config.json'
This works fine if I try to import a JavaScript file.
Embedding JSON into JavaScript programsstringify can now be used to generate valid ECMAScript string literals, object literals, and array literals. And because of the separate well-formed JSON.
x yes you can require your JSON just as you would require a js file.
This is a #1 google result for how to import JSON, and the main answer is to not import JSON. Having config in .js file is more convenient, because you can have comments inside. JSON file does not support this. Unfortunately, ES6/ES2015 doesn't support loading JSON via the module import syntax. But ... There are many ways you can do it.
Use an import assertion to import the JSON file. For example, import myJson from './example.json' assert {type: 'json'}. Here is my index.html file that has a script tag pointing to an index.js module. Copied! Make sure the type attribute is set to module, because we are using the ES6 Modules syntax.
There's no need to put it in a String. It's called JSON, not JSSN, after all. Also, torazaburo explained in a previously deleted answer: There is no ES6 "module system"; there is an API which is implemented by a particular loader. Any loader can do anything it wants, including supporting the import of JSON files.
Depending on your build tooling and the data structure within the JSON file, it may require importing the default. Added note for TypeScript is to ensure resolveJsonModule is true in you tsconfig.json. At the moment, we can't import files with a JSON mime type, only files with a JavaScript mime type.
In TypeScript or using Babel, you can import json file in your code.
// Babel import * as data from './example.json'; const word = data.name; console.log(word); // output 'testing'
Reference: https://hackernoon.com/import-json-into-typescript-8d465beded79
Importing JSON using ES modules was submitted as feature to TC39 in mid 2020, and is (at the time of this edit) in stage 3, which is the last stage before being accepted in to the spec (see https://github.com/tc39/proposal-json-modules for more details). Once landed, you will be able to use it as:
import someName from "./some/path/to/your/file.json";
Where someName
is effectively the name of the variable for the JS object described by the JSON data. (And of course, note that this imports JavaScript from a JSON source, it does not "import JSON").
If you're using a modern enough bundler (like esbuild
or the like) or you're using a recent enough transpiler (like babel
) then you can already use this syntax without having to worry about support.
Alternatively, if you have the luxury of ownership of JSON files you can also turn your JSON into valid JS files with a minimum of extra code:
config.js
export default { // my json here... }
then...
import config from '../config.js'
does not allow import of existing .json files, but does a job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With