
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to read an external JSON file in JavaScript
Suppose we have a JSON file config.json that contains the following data −
{ "secret": "sfsaad7898fdsfsdf^*($%^*$", "connectionString": "mongodb+srv://username:password@cluster0.laaif.mongodb.net/events?retryWrites=tr ue&w=majority", "localConnectionString": "mongodb+srv://username:password@cluster0.laaif.mongodb.net/eventsLocal?retryWrit es=true&w=majority", "frontendClient": "https://helloworld.com", "localFrontendClient": "http://localhost:3000" }
And in the same directory (folder), we have a JavaScript file index.js.
Our task is to access the content of the json file through the JavaScript file.
Method 1: Using require module (NodeJS environment only)
We can use the require module to access the json file if we are running our JavaScript file in NodeJS environment.
Example
The code for this will be −
const configData = require('./config.json'); console.log(typeof configData); console.log(configData);
Method 2: Using ES6 import module (Web Runtime Environment only)
If we want to access the json file while running JavaScript in browser, we can use the ES6 import syntax to do that.
Example
The code for this will be −
HTML FILE:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>READ JSON FILE</title> </head> <body> <p id='main'></p> <script type="module" src="./index.js"></script> </body> </html>
JAVASCRIPT FILE:
import configData from './config.json'; document.getElementById('main').innerHTML = JSON.stringify(configData);
Output
And the output will be −
{ secret: 'sfsaad7898fdsfsdf^*($%^*$', connectionString: 'mongodb+srv://username:password@cluster0.laaif.mongodb.net/events?retryWrites=tr ue&w=majority', localConnectionString: 'mongodb+srv://username:password@cluster0.laaif.mongodb.net/eventsLocal?retryWrit es=true&w=majority', frontendClient: 'https://helloworld.com', localFrontendClient: 'http://localhost:3000' }
- Related Articles
- How to write JavaScript in an External File?
- How to read JSON file in Python
- How to read an image file in external storage with runtime permission in android?
- How can we read a JSON file in Java?
- How to read a txt file in external storage with runtime permission in android?
- How to read the contents of a JSON file using Java?
- How to read data from JSON array using JavaScript?
- How to use external “.js” files in an HTML file?
- How to add a JSON string to an existing JSON file in Java?
- How to read a JSON file into a DataFrame using Python Pandas library?
- How to read data from *.CSV file using JavaScript?
- How to read and write a file using Javascript?
- How to put a Pandas DataFrame into a JSON file and read it again?
- How to define the URL of an external script file in HTML5?
- How to import local json file data to my JavaScript variable?

Advertisements