javascriptnode.jsjsonosc

convert json to osc address and arguments


I am trying to make a universal function in javascript, that converts a json data structure to an OSC compatible format. OSC meaning '/' seperated address strings assigned to arguments of any type.

A nested json like this:

{
  "hello":"world",
  "one":{
    "two":{
      "three":[4, 5, 6, 7]
    },
    "deux":"trois",
    "zwei":3
  }
}

would result in:

[
  {
    "addr":"/hello", 
    "args":"world"
  },
  {
    "addr":"/one/two/three", 
    "args":[4, 5, 6, 7]
  },
  {
    "addr":"/one/deux", 
    "args":"trois"
  },
  {
    "addr":"/one/zwei", 
    "args":3
  },
]

I'm not a fan of recursive functions, but I thought that's the only way to go, so I came up with this:

example = {
  "hello":"world",
  "one":{
    "two":{
      "three":[4, 5, 6, 7]
    },
    "deux":"trois",
    "zwei":3
  }
}

toOSC(example)

function toOSC(json) {
  var osc_msg = [{address:""}]
  createPath(json, osc_msg,0,"")
  for(let o of osc_msg) {
    if(o.hasOwnProperty('args')) {
      console.log(o)
    }
  }
}

function createPath(obj, osc_msg, i, addr) {
  for(let m in obj) {
    osc_msg[i]['address'] += '/' + m

    if(Array.isArray(obj[m]) || typeof obj[m] !== 'object') {
      osc_msg[i]['args'] = obj[m]
      i++
      osc_msg.push({address:""})
    } else {
      i = createPath(obj[m], osc_msg, i, osc_msg[i].address)
      i++
      osc_msg.push({address:addr})
    }
  }
  return i
}

The code fails in the way that the second of two nested objects of the same depth, gets rid of the first part of its address and I can't get my head around it.

I'm happy for any idea, also concerning the general approach to convert json to an OSC compatible format.

I want to use the conversion for sending messages with node.js package osc-min.


Solution

  • It is way easier if you pass down the previously traversed keys and yield up the results:

         function* format(obj, previous = "") {
           for(const [key, value] of Object.entries(obj)) {
             if(typeof value !== "object" || Array.isArray(value)) {
               yield { addr: previous + "/" + key, args: value };
             } else {
               yield* format(value, previous + "/" + key);
            }
          }
        }
    
        // That can be used as:
    
         const result = [...format({ a: { b: "test", d: { e: 1 }}, c: [1, 2, 3] })];
         
         console.log(result);