Defining a variable globally on the .json

So I wanted to declare the variable array colors globally across my project file , so I don't have to declare it for each of the widgets they use it (actually 64 of the widgets).
Knowing that .json doesn't do that as its a static data structure, I've checked and you can use jq for that: jq Manual (development version)
(Although I'm still not sure how to perfectly implement it)
I feel like I missed a point in the Documentation of the Program, which you can do this, but my poor knowledge of JavaScript parse the Documentation lines in my head in a little bit of a gibberish language.

How would you do it?

Thanks

(See example simplified code below)

          {
            "type": "button",
            "top": 40,
            "left": 20,
            "id": "1-1",
            (...)
            (...)
            "colorWidget": "JS{{\n\nvar velocityReceived = OSC{/note};\nvar colors = {1: \"#990000\", 5: \"#990000\", 9: \"#990000\", 13: \"#990000\", 2: \"#dd0000\", 6: \"#dd0000\", 10: \"#dd0000\", 14: \"#dd0000\", 3: \"#ff0000\", 7: \"#ff0000\", 11: \"#ff0000\", 15: \"#ff0000\", 16: \"#009900\", 20: \"#009900\", 24: \"#009900\", 28: \"#009900\", 32: \"#00dd00\", 36: \"#00dd00\", 40: \"#00dd00\", 44: \"#00dd00\", 48: \"#00ff00\", 52: \"#00ff00\", 56: \"#00ff00\", 60: \"#00ff00\", 17: \"#999900\", 21: \"#999900\", 25: \"#999900\", 29: \"#999900\", 33: \"#99dd00\", 37: \"#99dd00\", 41: \"#99dd00\", 45: \"#99dd00\", 49: \"#99ff00\", 53: \"#99ff00\", 57: \"#99ff00\", 61: \"#99ff00\", 18: \"#dd9900\", 22: \"#dd9900\", 26: \"#dd9900\", 30: \"#dd9900\", 34: \"#dddd00\", 38: \"#dddd00\", 42: \"#dddd00\", 46: \"#dddd00\", 50: \"#ddff00\", 54: \"#ddff00\", 58: \"#ddff00\", 62: \"#ddff00\", 19: \"#ff9900\", 23: \"#ff9900\", 27: \"#ff9900\", 31: \"#ff9900\", 35: \"#ffdd00\", 39: \"#ffdd00\", 43: \"#ffdd00\", 47: \"#ffdd00\", 51: \"#ffff00\", 54: \"#ffff00\", 58: \"#ffff00\", 62: \"#ffff00\" }\nreturn colors[velocityReceived] || \"#E0E0E0\";\n\n}}",
            "colorStroke": "auto",
            (...)
            (...)
          },
          {
            "type": "button",
            "top": 40,
            "left": 120,
            "id": "2-1",
            (...)
            (...)
            "colorWidget": "JS{{\n\nvar velocityReceived = OSC{/note};\nreturn colors[velocityReceived] || \"#E0E0E0\";\n\n}}",
            "colorStroke": "auto",
            (...)
            (...)
          },
 

You could simply create a "variable" widget to hold that array in its value and retrieve it anywhere using the inheritance syntax: @{variablewidget_id}

1 Like

Thank you,
I think I have a little issue with the fact that the inheritance syntax code does actually parse the escape characters.

So

{
            "type": "variable",
            "id": "colors-array",
            "value": "{1: \"#990000\", 55: \"#990000\" }",
}

When I retrieve the value from the widgets they have lost the \ before the " .

I'm checking and apparently there is a way to solve this, with JSON.stringify() . Still I have to find the way to use it correctly

{
  "value": {"1": "#990000", "55": "#990000" }
}

Works (JSON Syntax)

1 Like

Thanks very much.

I've got a type Error : cannot read property 'undefined' of undefined . But the whole think is Working .
Thanks!!