push raw JSON from node.script to a dict?
I'm receiving a JSON from a command line process (iperf3) which I'm running as a child process in [node.script]. How would I send it directly to a dict object in max?
Is it an option for you save that json to a file? If so, you could then send a message "import yourSavedFile.json" to a dict object
I also just remembered you can access a dict object via js, I'm not sure how it differs in `node.script`, but in a regular [js] object, you could do:
var jsonString = JSON.stringify(jsonObjectFromSomewhere);
var d = new Dict("dict_name");
d.parse(jsonString);
where "dict_name" is the name of the existing [dict] object you want to write to, and "jsonObjectFromSomewhere" is the json object you want to put in the dict.
In node script you can just do:
setDict(id, content); //where id is the name of the dict in Max and content is your parsed JSON (it can be any js object).
You first need to parse the JSON to an object with JSON.parse of course.
Heads up that its an asynchronous function.
thank you so much to both of you!
for me, the solution was:
Max.setDict("id", JSON.parse(content))
following up on this now, using nodejs, what if I wanted to append an object to a dict that already has some content? i'd assumed it'd be something like Max.appendDict, but its not
Hey.
you could utilise the updateDict API method that allows you to update a portion of a dict in Max using a "path" within the dict. Hereby you give the id of the dict, the path to update and the new data. The language can be a bit confusing but let's take two examples:
const maxApi = require("max-api");
const dictId = "my_dict";
const main = async () => {
await maxApi.setDict(dictId, {
"a": "first"
});
// my_dict =>
// {
// "a": "first"
// }
// append to dict
await maxApi.updateDict(dictId, "b", "second");
// my_dict =>
// {
// "a": "first",
// "b": "second",
// }
// add nested path
await maxApi.updateDict(dictId, "c.deeply.nested.prop", "third");
// my_dict =>
// {
// "a": "first",
// "b": "second",
// "c": {
// "deeply": {
// "nested": {
// "prop": "third"
// }
// }
// }
// }
// this even works with arrays
await maxApi.updateDict(dictId, "d", []);
await maxApi.updateDict(dictId, "d[0]", "fourth");
}
await main();
Does this answer your question?
Florian
yep, this is really helpful, Florian. thankyou!
one note is that the final line 'await main()' threw an error. so i removed the 'await' and it works very well now.
thank you also for the detailed documentation.
side note, was great to meet you in person at expo74 :D
Glad it helped and yeah, good catch. Sorry that unsupported top level await sneaked in ;)
And likewise, pleasure to chat IRL at expo74
-f