How to access my local JSON file in my JS script?

Agent67

Honorary Master
Joined
Nov 7, 2020
Messages
28,008
Reaction score
20,150
Location
Western Cape
So I need to access and get the contents of a local JSON file that contains the names of each staff member, their job title and an image of their face, so I can use JS to load all of that onto the related page. The problem is I tried to use fetch at first but it blocked me because of CORS.

I can't make a server, because if I do then it'll complicate things when I send in my assignment (yes, the same one mentioned in this post) and because the lecturer told me to stick close to the file submission requirements.

I can't put the JSON file contents into my script either because there has to be a separate "staff.json" file according to the requirements.

So I'm not sure what to do anymore. What do I do just to access the damned JSON file in my script?

P.S I already validated my JSON file on JSONLint, and I got a clean bill of health.
 
Can you load it via the “file://“ protocol?

Probably not. Browser is a sandbox. Being able to access a file would be extremely insecure.

You’d need to “upload” the file, using a file input. You can then access it via FormData
 
So I need to access and get the contents of a local JSON file that contains the names of each staff member, their job title and an image of their face, so I can use JS to load all of that onto the related page. The problem is I tried to use fetch at first but it blocked me because of CORS.

I can't make a server, because if I do then it'll complicate things when I send in my assignment (yes, the same one mentioned in this post) and because the lecturer told me to stick close to the file submission requirements.

I can't put the JSON file contents into my script either because there has to be a separate "staff.json" file according to the requirements.

So I'm not sure what to do anymore. What do I do just to access the damned JSON file in my script?

P.S I already validated my JSON file on JSONLint, and I got a clean bill of health.

With modern browsers you can't load file system files like that anymore, specifically due to CORS.
You have 3 options:
1. Use the http-server npm package which is an easy to use and lightweight http server.
2. Put the json contents in a js file, assign to a global variable and access like that.
3. Upload the file as per _kabal_
 
1. Use the http-server npm package which is an easy to use and lightweight http server.
I can't make a server, because if I do then it'll complicate things when I send in my assignment (yes, the same one mentioned in this post) and because the lecturer told me to stick close to the file submission requirements.
2. Put the json contents in a js file, assign to a global variable and access like that.
I can't put the JSON file contents into my script either because there has to be a separate "staff.json" file according to the requirements.
Upload the file as per _kabal_
I'll check it out.
 
 

Were you referring to something like this?
 

Were you referring to something like this?
 
Btw, all I am doing here are google searches like “read local file contents in browser” ;)
 

Basically no modern browser will allow you to load up a file without it being a web server.
Chat with your lecturer, ask him if he is fine with just a normal file input, or if can embed. Forcing an npm run is probably overdoing it for a lot of students in your year as then need to learn how to set up npm/webpack etc.
 
I'm just going to ask my lecturer again tomorrow if I can use a server. I've looked everywhere and all the answers I find are variations of fetch/AJAX. I'm not sure how the FileReader thing would work either.

Btw:

s.JPG

How can I have this display properly as HTML and not an HTML string? I've tried to appendChild with trying to escape the HTML but it just gives me errors. Hence why I'm using textContent for now.

1664094894554.png

@_kabal_
 
I'm just going to ask my lecturer again tomorrow if I can use a server. I've looked everywhere and all the answers I find are variations of fetch/AJAX. I'm not sure how the FileReader thing would work either.

Btw:

View attachment 1389180

How can I have this display properly as HTML and not an HTML string? I've tried to appendChild with trying to escape the HTML but it just gives me errors. Hence why I'm using textContent for now.

View attachment 1389188

@_kabal_
1664095319030.png
 
I'm just going to ask my lecturer again tomorrow if I can use a server. I've looked everywhere and all the answers I find are variations of fetch/AJAX. I'm not sure how the FileReader thing would work either.

Btw:

View attachment 1389180

How can I have this display properly as HTML and not an HTML string? I've tried to appendChild with trying to escape the HTML but it just gives me errors. Hence why I'm using textContent for now.

View attachment 1389188

@_kabal_
The file reader api document that I linked has an actual working example in it???
It works.

“Just gives me errors” is not a very useful statement.

Fetch/Ajax will not work.

Code:
const jsonObjectLoadedFromFile = JSON.parse(fileReader.result);
console.log(jsonObjectLoadedFromFile);

Adapt the example to do that first
 
Last edited:
The file reader api document that I linked has an actual working example in it???
It works.
The problem is I'm struggling to understand the example.
Fetch/Ajax will not work.

Code:
const jsonObjectLoadedFromFile = JSON.parse(fileReader.result);
console.log(jsonObjectLoadedFromFile);

Adapt the example to do that first
I'll try it again.

Btw any advice on the HTML string problem?
“Just gives me errors” is not a very useful statement.
Ok, the specific error I got is similar to this:
Node.appendChild: Argument 1 is not an object.
If I try to append the table just like that to the section.
 
The problem is I'm struggling to understand the example.

I'll try it again.

Btw any advice on the HTML string problem?

Ok, the specific error I got is similar to this:

If I try to append the table just like that to the section.

Get the example running in your own html/browser first.

Then you can break down the pieces. If you try to understand something that you cannot see in action, it is going to be more tricky to internalize the flow.

For your error: https://www.w3schools.com/jsref/met_node_appendchild.asp

appendChild needs an element, not a string
 
Get the example running in your own html/browser first.

Then you can break down the pieces. If you try to understand something that you cannot see in action, it is going to be more tricky to internalize the flow.

For your error: https://www.w3schools.com/jsref/met_node_appendchild.asp

appendChild needs an element, not a string
Ok, yet I save the table just as it is to sessionStorage, and try to appendChild but I get the same error? That I mentioned?
1664099388997.png
1664099412244.png
 
Ok, yet I save the table just as it is to sessionStorage, and try to appendChild but I get the same error? That I mentioned?
View attachment 1389226
View attachment 1389228
I got it. Did this:
JavaScript:
let tableDiv = document.createElement("table");
    console.log(getFromSessionStorage(keys[9]));
    servicesDiv.appendChild(tableDiv);
    tableDiv.innerHTML = getFromSessionStorage(keys[9]);
 
Top
Sign up to the MyBroadband newsletter
X