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
184,084 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
This is so wrong!
OK, but your file isn't JSON. It's JSON wrapped in Javascript "date ="
Hi Niroj,

I have a JSON file as below

{

"name":"Akash",

"age":20,

"dp":"..\\img\\dp.png",

"interest":[

"Sports", "Music", "Books"

]

}

Now, how can I parse this JSON.

I need to feed some variables in js, vName, vAge, vdp.

Regards,

Sukhomoy
So the solution to load JSON is don't use JSON?  instead you have converted it to javascript, so it is no longer a valid JSON file.
How to read this JSON file by declaring it inside the Javascript file?

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
8,081 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,365 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
5,197 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,850 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,445 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
3,135 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
3,167 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
45,586 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
4,093 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,642 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