How to read an external local JSON file in JavaScript

–1 vote

I have saved a JSON file in my local system and created a JavaScript file in order to read the JSON file and print data out. Here is the JSON file:

{"resource":"A","literals":["B","C","D"]}

Let’s say this is the path of the JSON file: /Users/Documents/workspace/test.json.

Could anyone please help me write a simple piece of code to read the JSON file and print the data in JavaScript?

Sep 3, 2020 in Java-Script by kartik
• 37,510 points
168,810 views

1 answer to this question.

0 votes

Hello @kartik,

For reading the external Local JSON file (data.json) using javascript, first create your data.json file:

data = '[{"name" : "Niroj", "age" : "22"},{"name" : "Dey", "age" : "20"}]';
  1. Mention the path of the json file in the script source along with the javascript file.

    <script type="text/javascript" src="data.json"></script>
    <script type="text/javascript" src="javascrip.js"></script>
  2. Get the Object from the json file

    var mydata = JSON.parse(data);
    alert(mydata[0].name);
    alert(mydata[0].age);
    alert(mydata[1].name);
    alert(mydata[1].age);

Hope it helps!!

Sometimes to read external JSON file in javascript, you need to be a good Java programmer. To be the one you should look for one of the top Java training class.

Thank You!!

answered Sep 3, 2020 by Niroj
• 82,820 points
Thanks for this..

Related Questions In Java-Script

0 votes
1 answer

How to access PHP var from external javascript file?

Hello @kartik, You don't really access it, you ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,820 points
5,639 views
0 votes
1 answer

How to validate an email address in JavaScript

Hello @kartik, To validate email use the below ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,820 points
521 views
0 votes
1 answer

How to load local JSON file?

Hello @kartik, $.getJSON is asynchronous so you should do: $.getJSON("test.json", ...READ MORE

answered Aug 25, 2020 in Java-Script by Niroj
• 82,820 points
3,577 views
0 votes
1 answer

How to pass an array as a function parameter in JavaScript?

Hello @kartik, Use: const args = ['p0', 'p1', 'p2']; call_me.apply(this, ...READ MORE

answered Sep 4, 2020 in Java-Script by Niroj
• 82,820 points
971 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
17,173 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,820 points
2,017 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
1,724 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,820 points
25,751 views
0 votes
1 answer

How to include csrf_token() in an external js file in Laravel?

Hello @kartik, To resolve this error you can ...READ MORE

answered Jun 11, 2020 in Java-Script by Niroj
• 82,820 points
2,599 views
0 votes
1 answer

How to parse Excel file in Javascript/HTML5?

Hello @kartik, Below Function converts the Excel sheet ...READ MORE

answered Jun 19, 2020 in Java-Script by Niroj
• 82,820 points
12,442 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP