Reading JSON file in javascript in M4L

Peter Nyboer's icon

I have a Max Project that I used to create a M4L device.
In the project is javascript code that reads in a JSON file.

The problem is that Max for Live always throws an error when I try to read the file when I load the device in Ableton Live. If I run the patch from the project in Max, the .json file is read. If I edit the device from Live, then unfreeze it, then load it into a Live track, the .json file is read.

In short, it seems that if the device is frozen, the file can't be found. Is there a way such that javascript can find a file to read in a Max for Live device?

I've attached a zip of the project and of the M4L device.

The function that fails looks like this:

function basicread(){
    var path = "sysex_vars.json";
    memstr = "";
    var maxchars = 800;
    var f = new File(path,"read");
    f.open();
    if (f.isopen) {
        while(f.position<f.eof) {
            memstr+=f.readstring(maxchars);
        }
        f.close();
        post("\nJSON Read Success: "+path);
    } else {
        post("\nJSON Read Error: "+path);
    }
}
JSreadtest.zip
application/zip 7.55 KB

JSreadtest.amxd.zip
application/zip 2.08 KB

tyler mazaika's icon

And the error is “file not found”? And the file is explicitly included in the Project window as well?

Peter Nyboer's icon

It's in the "data" folder of the Project and is included in the the device. Error is that it fails to read, so I'm assuming it's not found. It's a JS error, so it falls in the "else" of my code shown above.

Peter Nyboer's icon

Even more mind-boggling is that I can send a [text] object "read sysex_vars.json" and it loads, but using the javascript File object fails. So it appears Max is capable of finding it in the frozen file, but it is unavailable to the javascript File object.

tyler mazaika's icon

Yeah, File() seems to be doing not what I expect either. I think I battled this in the past with various attempts to construct the file path and then settled on something like this, which seems to work:

// Dict import_json works...
    var d = new Dict()
    d.import_json("sysex_vars.json")
    post(d.getkeys(),"\n")
    // Translate to js object
    var sysexvars = JSON.parse( d.stringify() )
    post( JSON.stringify(sysexvars),"\n")

Peter Nyboer's icon

Thank you!

Tory Rutter's icon

To read a JSON file in Max for Live (M4L), you can use JavaScript to load the file and parse its contents. Here's an example of how you can do this:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'path/to/file.json', true);
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
// Do something with the data
}
};
xhr.send();

In this example, an XMLHttpRequest object is created and used to load the JSON file at the specified path. The onreadystatechange function checks if the file has been successfully loaded and if its status is 200 (OK), and then parses the contents of the file using the JSON.parse method.

You can then access the properties of the JSON data using dot notation, like so:

console.log(data.property1);
console.log(data.property2);

Note that you'll need to replace 'path/to/file.json' with the actual path to your JSON file. If the file is located in the same directory as your M4L device, you can simply use the filename as the path.

Peter Nyboer's icon

Tory: that is a useful alternative, thanks. I was able to use the Dict import method successfully.