I am trying to load a JSON from a local input file into my JavaScript script.
Although I have successfully imported the JSON and obtained the file object, I am facing an issue with properly parsing the JSON and converting it into a JavaScript Object.
I attempted to use RequireJS as it seemed like the most efficient solution, but I am struggling to get this particular line of code to function correctly:
require(['json!someFile.json'], function(data){
})
This is the HTML input I am using:
<input type="file" class="custom-file-input" onchange="loadFile(this.file) accept="application/json">
And this is my JavaScript file:
function loadFile(file) {
var myJson;
var url = URL.createObjectURL(file);
require(['json!'+url+''], function(data){
myJson = data;
}
}
Alternatively, I also tried:
require(['json!'+file+''], function(data){