Read JSON from a Widget Script

Hi
in the custom-module I load the JSON file with
var vepKeys = loadJSON('../data/filemaker/vepKeys.json')
That works great in the custom-modul.

But is it possible to access the "vepKeys" Variable from a Widget per script?
In my script I tried read from "vepKeys" to define "s1","sk1","e1" and "ek1", but i have a syntax error.

var instrumentCode = String(get("text_code", value))
// console.log(instrumentCode)
var s1 = parseInt (vepKeys.keys [instrumentCode]['s1')
var sk1 = vepKeys.keys [instrumentCode]['sk1']
var e1 = parseInt (vepKeys.keys [instrumentCode]['e1'])
var ek1 = vepKeys.keys [instrumentCode]['ek1']

set ("range_2A_art_keyboard", [s1,e1 + 1])
set ("text_2A_art_range_key_start", [sk1])
set ("text_2A_art_range_key_end", [ek1])

You can't share variables between the custom module and widget scripts, these are two different and independant contexts. You need to send the json from the module to the widget, for example you could create a variable widget and call this in the custom module:

receive('/variable_widget_address', vepKeys)

This must called when the widget exists of course, for example using the app's sessionOpened event:

app.on('sessionOpened', (data, client)=>{
  receive('/variable_widget_address', vepKeys, {clientId: client.id)
})

And then you'll be able to read the json in scripts using

get('variable_widget_id')

Thanks for the quick response... I have problem to get to the data.

Ok, I defined the "variable_vepKeys" and this is working.
I tried several things, but how I get to the "e1" data in the JSON structure?.
Can you help?

var instrumentCode = String(get("text_code", value)) //This is the ItemNumber. Do i need a number instead of a string?
console.log (instrumentCode)
var test = get("variable_vepKeys", value) //to test that the JSON is all in
console.log (test)

var e1 = get("'variable_vepKeys'.'keys' [instrumentCode]['e1']") // How can I get the e1?
console.log (e1)

Here is my JSON Structure

This should be written

var e1 = get('variable_vepKeys').keys[instrumentCode].e1

or

var e1 = get('variable_vepKeys')['keys'][instrumentCode]['e1']

hmmmm...
I tried but I receive on both lines you gave me an error.

var instrumentCode = parseInt(get("text_code", value)) // I changed to parseInt to get a real number.
console.log (instrumentCode)
var e1 = get('variable_vepKeys').keys[instrumentCode].e1 // your text
console.log (e1)

I receive

my structure from my JSON looks like this
Bildschirmfoto 2022-11-02 um 15.00.09

Try

var instrumentCode = parseInt(get("text_code", value))
var vepKeys = get('variable_vepKeys')
if (typeof vepKeys === 'string') vepKeys = JSON.parse(vepKeys)
var e1 = get('variable_vepKeys').keys[instrumentCode].e1 
console.log (e1)

I copied your code and inserte it in my session... Same error

My Widget is a normal Button with articulation on it. And i use the newest v1.19.1 version. Only for your info...
Bildschirmfoto 2022-11-02 um 15.41.37

Is it a problem about the size? I have 5001 x 240 entries in...

I changed the 4th line to

var e1 = vepKeys.keys[instrumentCode].e1 

and it seems to work

Very thanks for this great help

the whole code looks like this

var instrumentCode = parseInt(get("text_code", value))
var vepKeys = get('variable_vepKeys')
if (typeof vepKeys === 'string') vepKeys = JSON.parse(vepKeys)
var s1 = vepKeys.keys[instrumentCode].s1 
var e1 = vepKeys.keys[instrumentCode].e1 
var sk1 = vepKeys.keys[instrumentCode].sk1
var ek1 = vepKeys.keys[instrumentCode].ek1
console.log (s1)
console.log (e1)
console.log (sk1)
console.log (ek1)

and the Result is correct.
Bildschirmfoto 2022-11-02 um 16.04.40

Oops you're right I forgot to use the vepKeys variable after updating it.

Good morning
My Variable-Widget was empty. I think this is because of your second line you gave me
and this is not implemented at the moment.

app.on('sessionOpened', (data, client)=>{
  receive('/variable_widget_address', vepKeys, {clientId: client.id)
})

Can I check somewhere more about the syntax?

SOLVED:
I used only this. Hope that is correct!

app.on('sessionOpened', (data, client)=>{
	receive('/variable_vepKeys', vepKeys)
})

Merci... Jean-Emmanuel

I have still synchronisation problems...

Here my configuration:

  • iMac with Cubase and OpenStageControl Server on IP 10.10.10.10
  • Touchscreen on a PC with Edge on IP 10.10.10.20

If I set on OpenStageControl (10.10.10.10) "no-gui" to true, the Widget Variable on the Touschscreen (10.10.10.20) will not update.
If I set on OpenStageControl (10.10.10.10) "no-gui" to false, the Widget Variable on the Touschscreen (10.10.10.20) is updated.

Do iI have to put the client.id from my Touchscreen in the app.on command?
And how I can get the client.id? (Is this a number or the IP Address?)

app.on('sessionOpened', (data, client)=>{
	receive('/variable_vepKeys', vepKeys)
})

the Widget Variable on the Touschscreen will not actualise.

What do you mean exactly ?

Do iI have to put the client.id from my Touscreen?

In my previous reply I showed how to use it, it's just used here to only send the message to the client that loaded the session and triggered the sessionOpened event.

1.) I make some changes in my vepKeys.json file.
2.) To reload the vepKeys.json I stop and restart the Open Stage Control Server.
3.) In my custom-modul I have following entry's:

var vepKeys = loadJSON('../data/filemaker/vepKeys.json')

// SET VARIABLE WIDGET
app.on('sessionOpened', (data, client)=>{
	receive('/variable_vepKeys', vepKeys)
})

This should update my variable_vepKeys on the widget, but it keeps the old entry from before.
Only when I stop my server again, change "no-gui" to false and start the server again, the "variable_vepKeys" on my Touchscreen is updated.

Do i have to set a timeout or how i can check that the session, not from the OSC Server, but from my touchscreen is open?

Here I have the custome-modul, hope I have the app.on command on the right place.
custom-module.js.zip (3.1 KB)

I found the problem. I have not only to start and stop the OSC Server, PLUS I have to reload the http browser to update the "variable_vepKeys".
Is it possible to put a widget-button to reload the own http browser on my touchscreen?

Add an onValue script to reload.

send("/RELOAD")

Does this work for you?

Cheers,
DMDComposer

sessionOpened is only triggered when the session is loaded, if you restart the server without reloading the client it won't get triggered.

Unfortunately not in my environment... but thanks

1 Like

It just occured to me you could get rid of the custom module part and put this in the variable's value property:

IMPORT{path/to/vepKeys.json} 
1 Like