
- 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 import local json file data to my JavaScript variable?
We have an employee.json file in a directory, within the same directory we have a js file, in which we want to import the content of the json file.
The content of employees.json −
employees.json
"Employees" : [ { "userId":"ravjy", "jobTitleName":"Developer", "firstName":"Ran","lastName":"Vijay", "preferredFullName":"Ran Vijay","employeeCode":"H9","region":"DL","phoneNumber":"34567689", "emailAddress":"ranvijay.k.ran@gmail.com" }, { "userId":"mrvjy","jobTitleName":"Developer","firstName":"Murli","lastName":"Vijay", "preferredFullName":"Murli Vijay","employeeCode":"A2","region":"MU", "phoneNumber":"6543565","emailAddress":"murli@vijay.com" } ] }
We can use any of the two ways to access the json file −
Using require module
Code to access employees.json using require module −
const data = require('./employees.json'); console.log(data);
Using fetch function
Code to access employees.json using fetch function −
fetch("./employees.json") .then(response => { return response.json(); }) .then(data => console.log(data));
Note − While the first function is better suited for node environment, the second function only works in the web environment because the fetch API is only accessible in the web environment.
After running any of the above using require or fetch function, the console output is as follows −
{ Employees: [ { userId: 'ravjy', jobTitleName: 'Developer', firstName: 'Ran', lastName: 'Vijay', preferredFullName: 'Ran Vijay', employeeCode: 'H9', region: 'DL', phoneNumber: '34567689', emailAddress: 'ranvijay.k.ran@gmail.com' }, { userId: 'mrvjy', jobTitleName: 'Developer', firstName: 'Murli', lastName: 'Vijay', preferredFullName: 'Murli Vijay', employeeCode: 'A2', region: 'MU', phoneNumber: '6543565', emailAddress: 'murli@vijay.com' } ] }
- Related Articles
- How to import a SVG file in JavaScript?
- How to import csv file data from Github in R?
- How to group JSON data in JavaScript?
- How to read an external JSON file in JavaScript
- How to link jQuery from my local machine?
- How to import csv file in PHP?
- How to retrieve data from JSON file using jQuery and Ajax?
- How to read data from JSON array using JavaScript?
- How to declare a local variable in Java?
- How to convert JSON file to CSV file using PowerShell?
- How can we import data from .txt file into MySQL table?
- How can we import data from .CSV file into MySQL table?
- How to refer and import SAP-UI-Core.js within my SAPUI5 project
- How to read JSON file in Python
- How to convert JSON data to a html table using JavaScript/jQuery?

Advertisements