# Context variable holding other context variables

**URL:** <https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624>\
**Category:** General\
**Created:** [25 June 2021 06:58 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624 "2021-06-25T06:58:06Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [25 June 2021 06:58 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/1 "2021-06-25T06:58:07Z")

</div>

I have a context variable and would like to create a JSON object as a new context variable where the previous context variable value is inserted. How should that be written?

Imagine the simple example below

The first inject node just sends a "Hello" string to a change node that sets the received payload to the flow.out variable. The second inject node should trigger the creation of a new variable as a JSON object where the value of the first should be included. But obviously I fail in trying different variants like

`{"captured":flow.get('out')}` (a wish this should work but it will of course not)

In JSONATA the method to do this is simple, `$flowContext('out')` but what would be the correct syntax in my example above?

Greatful for any hint!

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/880c7667fdde036e836bb5aabd615a872d543edb.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/2/f2b072d346cb52665414a6fac57d37aaa5d08a0c.png)

---

<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:** [25 June 2021 07:14 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/2 "2021-06-25T07:14:08Z")

</div>

> [@krambriw](#):
>
> In JSONATA the method to do this is simple, `$flowContext('out')` but what would be the correct syntax in my example above?

Have you tried ...

```auto
{"captured": $flowContext('out')}

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [25 June 2021 08:30 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/3 "2021-06-25T08:30:14Z")

</div>

Agree, that would be nice if it worked

But it gives  
_"Invalid 'to' JSON property"_

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [25 June 2021 08:33 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/4 "2021-06-25T08:33:44Z")

</div>

Also tried below but no 😞  
`{"captured": "$flowContext('out'), value"}`

---

<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:** [25 June 2021 08:35 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/5 "2021-06-25T08:35:19Z")

</div>

Works fine for me...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3581d9e6a9510365d13de136aa05d054c053e61.png)

```auto
[{"id":"eaeef752fb45e58c","type":"inject","z":"3422959b.40de62","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1720,"y":80,"wires":[["33a6944a1892c763"]]},{"id":"33a6944a1892c763","type":"change","z":"3422959b.40de62","name":"","rules":[{"t":"set","p":"out","pt":"flow","to":"hello","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"{\"captured\": $flowContext('out')}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1730,"y":140,"wires":[["8d537c9c2c79c9fe"]]},{"id":"8d537c9c2c79c9fe","type":"debug","z":"3422959b.40de62","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1730,"y":200,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [25 June 2021 08:46 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/6 "2021-06-25T08:46:11Z")

</div>

Well, in my example I wanted the result pushed to a new flow variable directly, creating a new JSON object, and not "walk the distance" via JSONATA to msg.payload and finally push it to context. You got me anyway on a working track, so thanks! With that "extra step" like below, it works

But it would be preferrable if it could be done in one single shot. I think...  
Best regards, Walter

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1cd80a86f4473705eec3a9831953cc2e9b47db96.png)

---

<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:** [25 June 2021 08:48 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/7 "2021-06-25T08:48:36Z")

</div>

> [@krambriw](#):
>
> But it would be preferrable if it could be done in one single shot. I think...

Like this?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81cb3b96d7cabf6b6306947712ef7828c10458fc.png)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [25 June 2021 09:03 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/8 "2021-06-25T09:03:04Z")

</div>

No, the out result shall be a JSON object, not a string

---

<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:** [25 June 2021 09:08 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/9 "2021-06-25T09:08:33Z")

</div>

Sorry Walter, you lost me.

Both your example and the one line entry I just posted will create a JavaScript object stored in `flow.in` - there is no difference.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30d19c3cfd9d71b82dcd43f54c775e3843821d91.png)

```auto
[{"id":"eaeef752fb45e58c","type":"inject","z":"3422959b.40de62","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1720,"y":80,"wires":[["33a6944a1892c763"]]},{"id":"33a6944a1892c763","type":"change","z":"3422959b.40de62","name":"","rules":[{"t":"set","p":"out","pt":"flow","to":"hello","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"{\"captured\": $flowContext('out')}","tot":"jsonata"},{"t":"set","p":"in","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1730,"y":140,"wires":[[]]},{"id":"21debea693fd511f","type":"inject","z":"3422959b.40de62","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1720,"y":200,"wires":[["0f0861ea0272e6aa"]]},{"id":"0f0861ea0272e6aa","type":"change","z":"3422959b.40de62","name":"","rules":[{"t":"set","p":"in2","pt":"flow","to":"{\"captured\": $flowContext('out')}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1720,"y":260,"wires":[[]]}]

```

Perhaps I am missing something or I misunderstood your requirement.

* * *

_PS for clarity, JSON object is not a thing. JSON is a string representation of a JavaScript object._

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [25 June 2021 09:14 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/10 "2021-06-25T09:14:42Z")

</div>

> [@krambriw](#):
>
> the out result shall be a JSON object, not a string

[This post](https://dev.to/desoga/javascript-object-vs-json-demystified-494j) should help to clarify the difference between Javascript objects and JSON.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [25 June 2021 09:19 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/11 "2021-06-25T09:19:04Z")

</div>

Yeah, I'm expressing myself unclear, whatever, I want to set it in an expression like below. And that is not working

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/970d2c54b0663bbd8d6436d8dd40c0a19d63ae46.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [25 June 2021 09:20 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/12 "2021-06-25T09:20:57Z")

</div>

That is not working because you have the type set to "JSON". Change the type to JSONata Expression.

---

<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:** [25 June 2021 09:23 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/13 "2021-06-25T09:23:57Z")

</div>

> [@krambriw](#):
>
> And that is not working

As Nick said and as I pointed out earlier...

> [@Steve-Mcl](#):
>
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/81cb3b96d7cabf6b6306947712ef7828c10458fc.png)
> 
> image922×308 12.3 KB

↑ note the `J:` in the screenshot and in the demo flow I posted.

Are you all sorted now Walter?

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [25 June 2021 09:39 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/14 "2021-06-25T09:39:11Z")

</div>

Yes, thank you all !!

Best regards, Walter

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [9 July 2021 09:40 UTC](https://discourse.nodered.org/t/context-variable-holding-other-context-variables/47624/15 "2021-07-09T09:40:01Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
