Read & Write JSON file || Basics
-
Im learning how to build my own sub-menus and im running into a couple of things I need advice on.
I've learned how to use the minimal FileSystem api's that allow me to create a file and read from a file. Im only good for one liners only at this point. lol
-
How do I now create a JSON file filled with values,
-
How do I access them?
Anybody care to share a run down on this process please!
End goal:
Create an object with key value pairs ' name : "string" ' , call up the value based on the name in a combobox, and apply the value to a component.
-
-
// Create an object const myObj = {"name": "Dave", "age": 38, "country": "England"}; // Save object to a file on desktop inline function saveObject(obj) { local f = FileSystem.getFolder(FileSystem.Desktop).getChildFile("myFile.json"); f.writeObject(obj); } // Load the file inline function loadObject() { local f = FileSystem.getFolder(FileSystem.Desktop).getChildFile("myFile.json"); if (!f.isFile()) return {}; // Invalid file, return empty object return f.loadAsObject(); // What you will have here is the same as the object you started with }
-
@d-healey Thank you!
check my edit just in case.
-
@d-healey Im writing to a .json file and saving into UserPresets root folder. Is that all good?
-
@Chazrox said in Read & Write JSON file || Basics:
UserPresets root folder. Is that all good?
You should only put user presets in there. If you want to save to a fixed location use the app data folder. https://docs.hise.dev/scripting/scripting-api/filesystem/index.html
-
@d-healey I was trying that one and I couldnt find where the file was going to.
Is AppData a folder within the plugin or is that a system folder?
-
@Chazrox said in Read & Write JSON file || Basics:
Is AppData a folder within the plugin or is that a system folder?
It's a system folder - it's where the UserPresets folder lives, among other things.
- GNU/Linux: /home/username/.config/Company Name/Plugin Name/
- Windows: Users/username/AppData/Roaming/Company Name/Plugin Name/
- MacOS:~/Library/Application Support/Company Name/Plugin Name/
You can open it from the File menu in HISE btw.
-
@d-healey got it.
So I got that much working, the part im missing is....
How do I add new pairs to the object after its created?
-
wait dont give me the answer yet....im getting it. lol.
-
@Chazrox said in Read & Write JSON file || Basics:
wait dont give me the answer yet....im getting it. lol.
hint - arrays are json "objects" too
-
-
// create an array for objects const var myObjectSet = []; // Create an object const myObj = {"name": "Dave", "age": 38, "country": "England"}; myObjectSet.push(myObj); const myObj2 = {"name": "Lindon", "age": 150, "country": "England"}; myObjectSet.push(myObj2); Console.print(trace(myObjectSet));
-
@Lindon nice! That was it! I get it now.
I just recently got comfortable sorting, cloning, flipping arrays so if this is anything like that im off to a good start.
Huge breakthrough for me here. haha.Thank You @d-healey & @Lindon !
@Lindon said in Read & Write JSON file || Basics:
hint - arrays are json "objects" too
So are all array api's available for these too?
-
C Chazrox marked this topic as a question
-
C Chazrox has marked this topic as solved
-
@Chazrox said in Read & Write JSON file || Basics:
So are all array api's available for these too?
Arrays are just arrays - so yes the array API is applicable to them...but your array elements are "complex" objects so heaven knows how stuff like sort would work - if at all....
-
@Lindon Ok. I'll do some reading on that too.
@Lindon said in Read & Write JSON file || Basics:
heaven knows...
-
@Lindon said in Read & Write JSON file || Basics:
but your array elements are "complex" objects so heaven knows how stuff like sort would work - if at all....
Custom sort function
-
can you take a look at this please?
// Im trying to add 'newPreset' to 'AttackTablePresets'const var AttackTablePresets = { "name": "36...............vOLVxe+....9izwDzO...f+....9C...vO", "name2": "36...............vOLVxe+....9izwDzO...f+....9C...vO", "name3": "36...............vOLVxe+....9izwDzO...f+....9C...vO" }; // SAVE PRESET BUTTON CONTROLS inline function onSaveOkControl(component, value) { if (value) { local tableData0 = TableEnvelope1Pro.exportAsBase64(0); local newPreset = {Newshit : tableData0}; AttackTablePresets.insert(newPreset); // Im trying to add 'newPreset' to 'AttackTablePresets' Console.print("START TRACE" + trace(myObjectSet) + "END TRACE"); } }; Content.getComponent("SaveOk").setControlCallback(onSaveOkControl);
-
const myObj = {"name": "Dave", "age": 38, "country": "England"}; // To add something new you can use dot notation myObj.newKey = newValue; // Or you can use bracket notation - only really needed if you have spaces in the key or are using a variable to hold the key myObj["newKey"] = newValue;
@Chazrox said in Read & Write JSON file || Basics:
AttackTablePresets.insert(newPreset);
This is for arrays, not objects. I think Lindon confused you :)
-
@d-healey oh wow..how do I find the rest of the dot.functions?
-
@Chazrox It's not a function, it's accessing the property of the object.
Just like when you use
.length
to find out how many characters are in a string or elements in an array. If it doesn't have()
at the end then it's not a function.You're going to love my scripting course :)