Parsing and writing datastructures variables

Hello.

I'd like to update a datastructure embedded into a variable from a widget, using the "onValue" script.

Getting one data from my structure ends up like this in my script:


var param = get("config").globalSettings.filter[get("filterSwitch")]["f" + getProp("parent", "variables").n];
console.log(param.freq)

A switch (filterSwitch) is used to toggle between left filter and right filter.
The parametric filter has each param named from f0 to f(n+1), in a matrix n = $

Then I can write to console, the value set into a datastructure written into a variable set by a custom module that loads a json file.

1st question: Is there a simplier way for getting the value, or just a simplier way for managing datastructures?
2nd question: how would I use setVar() to set the value?

1st question: Is there a simplier way for getting the value?

At first sight it doesn't look over-complicated but I think it would be easier to read if split into multiple lines / statements (getting back to these kind of one-liners after some time can be hard, your future self will thank you :)).

2nd question: how would I use setVar() to set the value?

Since you're retrieving it with get() in the first place you should use set() to modify it, but the idea would be the same anyway, all you need is to keep a reference to the datastructure itself so that you can pass it as the new value when you're done :

// get data structure
var config = get('config')
// get some object deeper in the structure
var param = config.globalSettings.etc
// editing param will affect config because it's 
// only a reference to its address in memory
// and not an actual copy of this object
config.globalSettings.etc.freq = 2000
// save modified object
set('config', config)
// editing param will affect config because it's 
// only a reference to its address in memory
// and not an actual copy of this object

Curiously, setting a new value to my object and using set() for writing the value doesn't affect the data structure and no error is prompted.

Lets say an object that looks like this :

{
  "filter": { "f0": {   "freq": 20 } }
}

this object is contained into a variable value called "testVar"

So I should be able to change "freq" value from another widget like this:

var config = get("testVar")
config.filter.f0.freq = 2000
set("testVar", config)

When I check data structure content, it still have freq fixed to 20 instead of 2000

At the same time I'd like to ask if I should try to dig into O-S-C state saving for updating my data structure?

When I check data structure content, it still have freq fixed to 20 instead of 2000

I just made a minimal test with the code you provided and it seems to work as expected : testVar.json (2.7 KB)

Could you build a minimal session that does not ?

I just made a minimal test with the code you provided and it seems to work as expected

In fact I was expecting to see result in structure displayed into value, and that's not happening with this minimal version.

I've got same result with my large structure, and forgot to mention that I can log the new value taken with get() method after the method set() has been applied.

Now I have to try out a good way to save this structure into a json file.

Just for the record, in custom module:

const fs = nativeRequire('fs');
module.exports = {
	oscOutFilter:function(data){
		var {address, args, host, port, clientId} = data
		const val = args[0].value;
		if (address === '/saveconfig') {
			fs.writeFileSync('../configTest.json', JSON.stringify(JSON.parse(val), null, 4));
			receive('/NOTIFY', 'triangle-exclamation','file written');
		}
    }
}

The content of the value property never changes when the actual value of the widget is updated, it's a static information that's part of the session like any widget property. However it could make sense to also display the actual value in that property's help window (like how the "computed" value is displayed).