Pull from a local json file?

I have a large array that I pull from for my API. Is it possible to move this to a JSON file that is also hosted with the worker?

Basically just asking if it is possible or how to move my array out of my index.js file into, for example, a data.json file also on the worker.

Thank you!

What is the size?

1 Like

only 5kb

If that small you could use global variable approach:

let data;
// as global variable 
if(data) {
const Resp = await fetch('http://example.com/data.json')
data = await Resp.json();

Thank you! I am aware I can fetch the data. I am just curious if the file can be uploaded with the worker.

Is this possible?

Then use a webpack approach

Is this read-only data? If it’s not changing, just append / prepend it to your JS as a global.

However, if you need to change it, the solutions are a little different depending on usecase:

Update occasionally by site owner: store it in KV, and update it using the CF API (not through Workers).

Actively update based on events: break it apart and store as individual discrete elements within the KV. Read and Write from within your Worker.

It is read-only. I do currently have it in my JS file. I was just wondering if I can separate it to make it cleaner.


you can pull online any json file. just go to: JSON to XML | JSON to XML Converter - Convert99.com