Loads a JSON file to create an Object
.
JavaScript Object Notation (JSON) is a standard format for sending data between applications. The format is based on JavaScript objects which have keys and values. JSON files store data in an object with strings as keys. Values can be strings, numbers, Booleans, arrays, null
, or other objects.
The first parameter, path
, is a string with the path to the file. Paths to local files should be relative, as in loadJSON('/assets/data.json')
. URLs such as 'https://example.com/data.json'
may be blocked due to browser security. The path
parameter can also be defined as a Request
object for more advanced usage.
The second parameter, successCallback
, is optional. If a function is passed, as in loadJSON('/assets/data.json', handleData)
, then the handleData()
function will be called once the data loads. The object created from the JSON data will be passed to handleData()
as its only argument. The return value of the handleData()
function will be used as the final return value of loadJSON('/assets/data.json', handleData)
.
The third parameter, failureCallback
, is also optional. If a function is passed, as in loadJSON('/assets/data.json', handleData, handleFailure)
, then the handleFailure()
function will be called if an error occurs while loading. The Error
object will be passed to handleFailure()
as its only argument. The return value of the handleFailure()
function will be used as the final return value of loadJSON('/assets/data.json', handleData, handleFailure)
.
This function returns a Promise
and should be used in an async
setup with await
. See the examples for the usage syntax.
Examples
Syntax
loadJSON(path, [successCallback], [errorCallback])
Parameters
Error
event object.