As of Node v0.5.x, yes.
You can require your JSON just as you would require a JS file.
const someObject = require('./somefile.json')
In ES6:
import someObject from './somefile.json'
Answer from goatslacker on Stack Overflow Top answer 1 of 11
621
As of Node v0.5.x, yes.
You can require your JSON just as you would require a JS file.
const someObject = require('./somefile.json')
In ES6:
import someObject from './somefile.json'
2 of 11
70
JSON files don’t require an explicit exports statement. You don't need to export to use it as Javascript files.
So, you can use just require for valid JSON document.
data.json
{
"name": "Freddie Mercury"
}
main.js
var obj = require('data.json');
console.log(obj.name);
//Freddie Mercury
Videos
03:48
How to read JSON file in JavaScript - YouTube
05:53
Node.js Tutorial - 17 - Importing JSON and Watch Mode - YouTube
08:16
How to Work with JSON Files in Node.js | Read, Write, and Update ...
01:40
How to Import a JSON File with Node.js - YouTube
05:35
NODE.JS TUTORIAL IMPORT JSON AND WATCH MODE #12 #nodejs #DATAHACKS ...
Stack Abuse
stackabuse.com › bytes › how-to-import-a-json-file-in-javascript-node-js
How to Import a JSON File in JavaScript/Node.js
September 18, 2023 - Also, require caches whatever it loads, so if you need to reload a JSON file later in your program execution, it'll still return the same data, even if the file changed. While importing JSON files in Node.js is generally simple, you still have to do proper error handling along the way.
Koladechris
koladechris.com › blog › how-to-import-a-json-file-in-javaScript
How to Import a JSON File in JavaScript
Learn how to import a JSON file in JavaScript, handle common errors, and explore a practical alternative.
Reddit
reddit.com › r/learnjavascript › how to import json files in js
r/learnjavascript on Reddit: How to import json files in js
May 30, 2023 -
Hi, is there any way to import .json file in to a js file like (import data from './data.json') ? I've tried that and also added <script type="module"/> in my html also still it was not working for me, I'd greatly appreciate to learn how to import json in my js file.
Thankyou in advance ;)
Top answer 1 of 2
3
Currently there is no way to directly import JSON files using JavaScript (ESM). You could load them in as data, but that would be going through a separate (usually asynchronous) API rather than using the module system such as fetch . If you're using a bundler (e.g. webpack, rollup, esbuild, etc.), they usually allow you to specify JSON in import statements from within your source code. Then, during bundling, those import statements are re-written to include your JSON through alternate means despite, to you, it appears to work no differently than any other source file. In the future we should be able to do this without the help of bundlers. For more information on that proposed feature, see: https://github.com/tc39/proposal-json-modules
2 of 2
2
I do it like this: const response = await fetch('./my_json_file.json'); const json = await response.json(); And that's it. Of course you'll have to perform it in an async function (unless you'd rather just use the .then() syntax), but this'll do the trick.
YouTube
youtube.com › watch
Nodejs Tutorial #25: Import JSON Data and Create Your First API - YouTube
✨LinkedIn: https://www.linkedin.com/in/khaiserkhanam/✨Instagram: https://www.instagram.com/khaiserkhanam/🎁Gift Business: https://www.instagram.com/gleam_cre...
Published January 11, 2025 Views 419
Futurestud.io
futurestud.io › tutorials › node-js-read-a-json-file
Node.js — Read a JSON File
const Fs = require('fs/promises') const json = await Fs.readFile('./package.json') const package = JSON.parse(json) package.version // 3.5.0 · You can use the package object like you would use any other JavaScript object. The CommonJS module loader comes with the require() function. It’s ...
Node.js
nodejs.org › api › esm.html
Modules: ECMAScript modules | Node.js v25.8.1 Documentation
Return the parsed JSON source of the file at pjsonURL. ... Parse source as an ECMAScript module. ... If source contains top-level await, static import or export statements, or import.meta, return true.