Array to Json Object

Hi @jackie7

Sorry to nitpick but Json object is not a thing.
JSON is a string representation of a JavaScript object.

Ok, enough of that :slight_smile:

Use a function node, loop through the elements & add properties to an object...

  var arr = msg.payload; // get the array
  var rv = {};// create a new empty object
  for (var i = 0; i < arr.length; ++i) {
    let key = "Val"+(i+1); //built a key 
    rv[key] = arr[i]; // set value in New object
  }
  msg.payload = nv;
  return msg;

There are other, more modern/compact ways to achieve this but I kept it simple for clarity.

Disclaimer: the above code is untested/off the top of my head. :innocent:

1 Like