Loading plain json , xml or csv(text) files from ZIM

Is there any example showing how to load a plain json file (or a text file) and use it in ZIM? Once the json is loaded from a file we can then use it with isJSON() or parseJSON()? (I understand that we can use </script to include a json variable..)

ZIM may not have any special Api for that, since it's a basic functionality. You can just use plain Javascript to do that.

fetch('file.json')
  .then(response => {
    if (!response.ok) {
      throw new Error('Some error');
    }
    return response.json();
  })
  .then(data => {
    console.log(data);
  })
  .catch(error => {
     console.error('Error:', error);
  });

Yes, i could have done this way. Loading couple json files will be more suitable with ZIM preload or ZIM lazy load. Temporary, i use your approach. Thanks for the support.

Sorry - missed this...

new Frame(FIT, 1024, 768, light, dark, ready, "test.json", "assets/");
function ready() {
	
	// given F (Frame), S (Stage), W (width), H (height)
	// put code here

	const data = asset("test.json");	
	new Label(data.test).center();

	// or 
	// new Label(asset("test.json").test).center();
			
}

with

{"test":"hello"} 

in the test.json file.

When we moved images and sound to Pic() and Aud() rather than using asset() (although we still can) and make SVG() and Vid() we thought about making a three word JSON wrapper... but what would we call it? So we just left it in asset() along with getting text in asset() too.

Yes. Either at preload or lazy-load level, almost any basic data type, now, can be easily handled from within ZIM. Tak you.

1 Like