# Get all node context values as one object

**URL:** <https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216>\
**Category:** General\
**Created:** [11 August 2019 13:49 UTC](https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216 "2019-08-11T13:49:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 August 2019 13:49 UTC](https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216/1 "2019-08-11T13:49:40Z")

</div>

I can use this

```auto
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

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

```

without having to manually program up a long concat statement

Any ideas on howto?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [11 August 2019 14:08 UTC](https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216/2 "2019-08-11T14:08:36Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 August 2019 16:02 UTC](https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216/3 "2019-08-11T16:02:09Z")

</div>

> [@Steve-Mcl](#):
>
> store all the variables in an object then store the object in the nodes context.

I've done as you suggested.- ta 🙂

For an intellectual 🙂 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

---

<div class="post-metadata">

**Author:** ![thatcadguy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/thatcadguy/32/5273_2.png) [@thatcadguy](https://discourse.nodered.org/u/thatcadguy)\
**Post date:** [11 August 2019 19:07 UTC](https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216/4 "2019-08-11T19:07:36Z")

</div>

You can just do:

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

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [11 August 2019 19:09 UTC](https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216/5 "2019-08-11T19:09:56Z")

</div>

I did 🙂

.........

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 August 2019 12:44 UTC](https://discourse.nodered.org/t/get-all-node-context-values-as-one-object/14216/6 "2019-08-12T12:44:25Z")

</div>

> [@Steve-Mcl](#):
>
> Alternatively, and arguably easier, store all the variables in an object then store the object in the nodes context.

Or you can use JSONata.

> **[Using JSONata to transform msg data](https://it.knightnet.org.uk/kb/nr-qa/jsonata/#recreate-the-msg-on-the-msg-payload)**
>
> JSONata is a syntax designed by IBM to help reformat and restructure JSON data in a similar vein to the way that XLST is used to transform XML data.

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