Get all node context values as one object

I can use this

msg['payload'] = context.keys();

to give me a nice array with all the all the node context keys but I'm after getting the values as well
e.g if I've to 3 keynames red,green and blue with values 0,1 and 2 - I'm after getting

{"red":0,"green":1,"blue":2}

without having to manually program up a long concat statement

Any ideas on howto?

There is a way (loop through the keys, retrieve the value & build an object.

Alternatively, and arguably easier, store all the variables in an object then store the object in the nodes context.

E.g.

var data = context.get("data") || {};
data.prop1 = 55;
context.set("data",data);
1 Like

I've done as you suggested.- ta :slight_smile:

For an intellectual :slight_smile: exercise I'm going to see if I can find the source code for context getting and see if its easy to add another method such as context.object() to save the (very minor) effort of creating the "dummy" data object

You can just do:

context.set("data.prop1", 55);

I did :slight_smile:

.........

Or you can use JSONata.

(PS: Sorry if you don't get the right formatting - I messed up my last update and need to recalculate the integrity digests)

1 Like