Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a JSON file in ECMAScript 6?

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.

like image 477
Nikita Jajodia Avatar asked Jan 22 '16 10:01

Nikita Jajodia


People also ask

Is JSON compatible with ECMAScript?

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.

Do you need to import JSON?

x yes you can require your JSON just as you would require a js file.

How to import JSON in ES6/ES2015?

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.

How do I import a JSON file into an HTML page?

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.

Is it possible to import a jssn string in ES6?

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.

Why can't I import a JSON file with typescript?

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.


Video Answer


2 Answers

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

like image 149
williamli Avatar answered Sep 20 '22 15:09

williamli


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.

like image 22
Gilbert Avatar answered Sep 22 '22 15:09

Gilbert