How to read an external local JSON file in JavaScript

0 votes

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,520 points
183,675 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,840 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,840 points
7,760 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,840 points
1,144 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,840 points
4,940 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,840 points
1,726 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,840 points
23,012 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,840 points
2,993 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,840 points
2,917 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,840 points
44,682 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,840 points
3,890 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,840 points
14,324 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