p5.js | loadJSON() Function
The loadJSON() function is used to read the contents of a JSON file or URL and return it as an object. In case the file contains a JSON array, this function would still return it as an object with the index numbers specifying the different keys of the object. This method can support file sizes up to 64MB.
This function is asynchronous, therefore it is recommended to be called in the preload() function to ensure that the function is executed before the other functions.
loadJSON(path, jsonpOptions, datatype, callback, errorCallback)
loadJSON(path, datatype, callback, errorCallback)
loadJSON(path, callback, errorCallback)
Parameters: This function accepts five parameters as mentioned above and described below:
- path: This is a string which denotes the file path or URL from where the JSON has to be loaded.
- jsonpOptions: This is an object which has options for settings related to “jsonp” . It is an optional parameter.
- datatype: It is a string which specifies whether the json object is “json” or “jsonp”. It is an optional parameter.
- callback: This is a function which is called when this function executes successfully. The first argument for this function is the data loaded from the file. It is an optional parameter.
- errorCallback: This is a function which is called if there is any error in executing the function. The first argument for this function is the error response. It is an optional parameter.
Return Value: It returns an object with the loaded JSON data.
The examples below illustrate the loadJSON() function in p5.js:
Online editor: https://editor.p5js.org/