Table of Contents

JSON.load (static)

A function to simplify loading JSON data over the network.

Usage:
JSON.load(url, options)
Member of:
JSON (as static method)
Parameters:
  1. url – the address to load the from from (required).
  2. options – an Object containing options for the HTTP request, as specified for the Fetch API (optional).
Returns:
A Promise object, as returned by the fetch() function.
Notes:
  • This is a static method of the JSON object. It has to be called literally as “JSON.load(…)”; it can not be called on any instance of JSON.
  • This is an asynchronous function, as the fetched data may only be available after a delay.
  • Security restrictions on which resources can be loaded, still apply to these requests. See CORS for more information.

Examples

Minimal example

JSON.load('file.json')
  .then( obj => { console.log(obj) });

This will load the file “file.json” and log its content to the console. It does not provide any error handling yet.

Example with error handler

Because this function returns a Promise object, we have access to the optional .catch() and .finally() methods, which are used in this example:

JSON.load('file.json')
  .then( result => {
    console.log(result);
  })
  .catch( error => {
    console.error(error);
  })
  .finally( () => {
    console.log("All done.");
  });

See also

More information