JSON.load (static)

A function to simplify loading [[wp>JSON|JSON]] data over the network.
Usage:
**JSON.load**(url, options)
Member of:
[[mdn>Web/JavaScript/Reference/Global_Objects/JSON|JSON]] (as //static// method)
Parameters:
- url – the address to load the from from (required). - options – an [[mdn>Web/JavaScript/Reference/Global_Objects/Object|Object]] containing options for the HTTP request, as specified for the [[mdn>Web/API/Fetch_API|Fetch API]] (optional).
Returns:
A [[mdn>Web/JavaScript/Reference/Global_Objects/Promise|Promise]] object, as returned by the [[mdn>Web/API/fetch|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 [[mdn>Learn/JavaScript/Asynchronous/Introducing|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 [[mdn>Web/HTTP/CORS|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 [[mdn>Web/JavaScript/Reference/Global_Objects/Promise|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 ===== * [[toolbox:element:load:index|.load()]] ===== More information ===== * [[mdn>Web/API/Fetch_API/Using_Fetch|Using Fetch]] on MDN. * [[mdn>Web/JavaScript/Guide/Using_promises|Using promises]] on MDN.