Context variable holding other context variables

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!


Have you tried ...

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

Agree, that would be nice if it worked

But it gives
"Invalid 'to' JSON property"

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

Works fine for me...


[{"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":[]}]

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


Like this?

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

Sorry Walter, you lost me.

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


[{"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.

This post should help to clarify the difference between Javascript objects and JSON.

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


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

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

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

Are you all sorted now Walter?

Yes, thank you all !!

Best regards, Walter

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