Converting payload.object to an array

object
array

hi,
I wanted to convert my payload.object in form of array. Please help
New to this Node red family.

You will need to specify what format you want, if you do a direct conversion, you will loose the property names of course.

In code, the simple way to do a direct conversion is:

const myarray = Object.values(msg.payload)

I want in this format. Where Id will come with topic and V will come with value.

There are many ways to restructure your JS object into an array of values. My personal favorite is to use a JSONata expression inside of a change node -- something like this:

Note: the JSONata expression is not Javascript... it's a lambda function that "spreads" each field of the object into an array of key:val pairs, then builds those into an array of objects with "id" and '"v" fields to hold the topic+key and the value, respectively:

payload.$spread().{
    'id': $$.topic & '.' & $keys()[0],
    'v': *
}

It's a lot to take in, I know, but a very powerful language to understand. If you are interested, check out docs.jsonata.org for more info.

1 Like