Create javascript object from osc /message

Hello!

In my switch widget's onValue, please help me understand why testObj2 works when declared literally, but testObj1 (created from an osc /message) doesn't.

Since some midi messages require more than one parameter, I'm unable to just use the switch's value and need more data for the midi messages that will be sent below depending on how the keyswitches are setup in Logic Pro. (The commented out send statements are unfinished 'code sketches'. I'll flesh those out later.)

Thanks!

var oscMsg = get('switchesObjectArray')
//console.log(oscMsg)
var data = JSON.stringify(oscMsg)
//console.log(data)
var testObj1 = JSON.parse(data)
//console.log(testObj1)
var testObj2 = {1002:["Note On",0,10],1001:["Note Off",127,20],1003:["Poly Aftertouch",0,30],1004:["Controller",32,40],1005:["Program",null,50],1006:["Aftertouch",null,60],1007:["Pitch Bend",null,70],1008:["Velocity",null,80],1009:["Note On",0,90]}
//console.log(testObj2)
var typeData = testObj2[value][0]
console.log(typeData)

var g_channel = get('g_midiCh')
//console.log(g_channel)


//if typeData value is "Note On"
//send('midi:OSC-Midi', '/note', g_channel, selector, 127)
//send('midi:OSC-Midi', '/note', g_channel, selector, 0)
//if typeData value is "Note Off"
//send('midi:OSC-Midi', '/note_off', g_channel, selector, 0)
//if typeData value is "Controller"
//send('midi:OSC-Midi', '/control', g_channel, selector, valueStart)
//if typeData value is "Program"
//send('midi:OSC-Midi', '/program', g_channel, valueStart)
//if typeData value is "Pitch Bend"
//send('midi:OSC-Midi', '/pitch', g_channel, valueStart)
//if typeData value is "Aftertouch"
//send('midi:OSC-Midi', '/channel_pressure', g_channel, valueStart)
//if typeData value is "Poly Aftertouch"
//send('midi:OSC-Midi', '/key_pressure', g_channel, selector, valueStart)

What's the output of

console.log('type: ' + typeof oscMsg)
console.log(oscMsg)

?

It's a string. Which is why I tried to stringify and parse to an object, but perhaps I'm not doing that right.

type: string
{1002:["Note On",0,10],1001:["Note Off",127,20],1003:["Poly Aftertouch",0,30],1004:["Controller",32,40],1005:["Program",null,50],1006:["Aftertouch",null,60],1007:["Pitch Bend",null,70],1008:["Velocity",null,80],1009:["Note On",0,90]}

Same result, type: string for testObj1 after JSON.parse.

Got it working with a switchesObjectArray OSC message of

{"1002":["Note On", 0, 10],"1001":["Note Off", 127, 20],"1003":["Poly Aftertouch", 0, 30],"1004":["Controller", 32, 40],"1005":["Program", null, 50],"1006":["Aftertouch", null, 60],"1007":["Pitch Bend", null, 70],"1008":["Velocity", null, 80],"1009":["Note On", 0, 90]}
// send midi switches to Logic
// lookup additional properties tied to value
var oscObj = JSON.parse(get('switchesObjectArray'))
//console.log('type: ' + typeof oscObj)
var type = oscObj[value][0]
//console.log(typeData)
var selector = oscObj[value][1]
//console.log(selectorData)
var valueStart = oscObj[value][2]
//console.log(valueStartData)

// get global properties from artSet.plist
var g_channel = get('g_midiCh')
//console.log(g_channel)
var g_octaveOffset = get('g_octaveOffset')
//console.log(g_octaveOffset)

// send proper switch to Logic
if (type === 'Note On') {
send('midi:OSC-Midi', '/note', g_channel, selector, valueStart)
send('midi:OSC-Midi', '/note', g_channel, selector, 0)
} else if (type === 'Controller') {
send('midi:OSC-Midi', '/control', g_channel, selector, valueStart)
} else if (type === 'Program') {
send('midi:OSC-Midi', '/program', g_channel, (valueStart - 1)) //-1 since Logic value 0 based
} else if (type === 'Pitch Bend') {
send('midi:OSC-Midi', '/pitch', g_channel, valueStart)
} else if (type === 'Aftertouch') {
send('midi:OSC-Midi', '/channel_pressure', g_channel, valueStart)
} else if (type === 'Poly Aftertouch') {
send('midi:OSC-Midi', '/key_pressure', g_channel, selector, valueStart)
} else if (type === 'Note Off') {
send('midi:OSC-Midi', '/note_off', g_channel, selector, 0)
//} else if (type === 'Velocity') {
//send('midi:OSC-Midi', '/???', g_channel, valueStart)
}