# How to use msg.payload string value in JSONata?

**URL:** https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443
**Category:** General
**Created:** [10 May 2021 19:47 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443 "2021-05-10T19:47:19Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)
#### Post date: [10 May 2021 19:47 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/1 "2021-05-10T19:47:20Z")

</div>

In this picture I use the word 'product' in the name:value pairs of payload.{ }  
Is it possible to set the name 'product' with the string value of a msg.?

By example. The msg.test message contains a string value: 'product'.  
Is it possible to use this message as the "name:value" pair?

I thought the JSONata must be like: payload.{msg.test: $}  
But this doesn't work... 🙄

I tried a lot of different things but doesn't have a clue to do this. 😕  
Can anyone help me?

 ![Schermafbeelding 2021-05-05 om 18.39.25](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/1485aea4bd25d24241e3ebae06c02f89f33b0ece.png)

---

<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: [10 May 2021 20:26 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/2 "2021-05-10T20:26:32Z")

</div>

Please share an example of the output msg JSON you are expecting to get. And an example copy of the input msg

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [10 May 2021 21:48 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/3 "2021-05-10T21:48:14Z")

</div>

Hello Wolff,

I am not pretty sure, if I understood you well, but did you check the helper function _**setObjectProperty(msg, prop, value, createMissing)**_ (ref. [Node-RED](https://nodered.org/docs/api/modules/v/1.3/@node-red_util_util.html#.setObjectProperty)) already? - Maybe this can help you.

If not, I would need more information to understand your problem. Maybe - as requested already - some more examples.

Cheers  
Ranki

---

<div class="post-metadata">

### Author: ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)
#### Post date: [10 May 2021 23:11 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/4 "2021-05-10T23:11:09Z")

</div>

> [@MrWolff](#):
>
> I thought the JSONata must be like: payload.{msg.test: $}  
> But this doesn't work... 🙄

If I understand your question, it may be as simple as this: `{ test: payload }`

Here is what it looks like in a `change` node, given the 2 fields `test` and `payload` as strings...

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

This works because the bare words "test" and "payload" are both replaced by their values in the msg object. So be careful to make sure the "test" string is valid for use as a field name in the newly created output object (so no spaces, newlines, and weird punctuation) -- or else the change node will fail.

As Julian suggests, giving examples of your input data (and desired output object) will go a long way to getting better help... For instance, your example expression shows `payload.{...}` which implies that your payload is an array. If that is the case, then the expression would have to pull the "test" value from the root of the message, so the expression would be something like `payload.{ $$.test: $ }`

When you use the dot `.` operator, each element of the array is used as the "context" of the expression inside curly braces (which is why `$` evaluates to the current element). To use another field on the msg which is at the same level as `payload` requires the `$$.` prefix to start the lookup at the top of the msg structure. Hope that makes sense.

---

<div class="post-metadata">

### Author: ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)
#### Post date: [11 May 2021 05:59 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/5 "2021-05-11T05:59:08Z")

</div>

The thing I want to do is:  
I have 2 dropdown menus. When I select the product in 'dropdown product' I want to select in the 'dropdown type' the product type.  
So when I select ball in 'product'. I want to select the type of ball in 'type'.

 ![Schermafbeelding 2021-05-10 om 18.13.53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e2daa0f5420981409af3496a7ac2c713f222f348.png)

I used a template node to simulate the following csv file:  
 ![Schermafbeelding 2021-05-10 om 18.10.55](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/1/4149cb80a477590c15aaa83995b3049f8e21151f.png)

This is the flow:

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

```auto
[{"id":"8c0623de.87ca1","type":"inject","z":"b4370c04.a6c7d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":240,"y":2640,"wires":[["3ce8055d.54a18a"]]},{"id":"3ce8055d.54a18a","type":"template","z":"b4370c04.a6c7d","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"product,ball,train,hat\nball,baseball,yellowtrain,bighat\ntrain ,football,bluetrain,smallhat\nhat,tennisball,redtrain,mediumhat","output":"str","x":280,"y":2680,"wires":[["a1092c24.2d9c8"]]},{"id":"a1092c24.2d9c8","type":"csv","z":"b4370c04.a6c7d","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":430,"y":2680,"wires":[["710b3dda.63a9b4","36d6405f.fb078","c348b469.19c818"]]},{"id":"710b3dda.63a9b4","type":"change","z":"b4370c04.a6c7d","name":"change1","rules":[{"t":"set","p":"options","pt":"msg","to":"payload.{product: product}","tot":"jsonata"},{"t":"set","p":"test","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":2680,"wires":[["41316f23.24fc1","86da049b.8aed18"]]},{"id":"41316f23.24fc1","type":"ui_dropdown","z":"b4370c04.a6c7d","name":"","label":"product","tooltip":"","place":"Select option","group":"7da9517d.305a1","order":1,"width":0,"height":0,"passthru":false,"multiple":false,"options":[{"label":"test","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","x":820,"y":2680,"wires":[["3fbad61f.c17efa","8835f550.6d4fb"]]},{"id":"3fbad61f.c17efa","type":"debug","z":"b4370c04.a6c7d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":2640,"wires":[]},{"id":"36d6405f.fb078","type":"debug","z":"b4370c04.a6c7d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":2640,"wires":[]},{"id":"c348b469.19c818","type":"change","z":"b4370c04.a6c7d","name":"change2","rules":[{"t":"set","p":"options","pt":"msg","to":"payload.{$$.test: $$.test}","tot":"jsonata"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":2800,"wires":[["28949ca7.79c294","6def0228.d2382c"]]},{"id":"28949ca7.79c294","type":"ui_dropdown","z":"b4370c04.a6c7d","name":"","label":"type","tooltip":"","place":"Select option","group":"7da9517d.305a1","order":2,"width":0,"height":0,"passthru":false,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","x":990,"y":2800,"wires":[[]]},{"id":"6def0228.d2382c","type":"debug","z":"b4370c04.a6c7d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":970,"y":2760,"wires":[]},{"id":"8835f550.6d4fb","type":"change","z":"b4370c04.a6c7d","name":"","rules":[{"t":"set","p":"test","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1030,"y":2680,"wires":[["a34961c7.b9cb78","c348b469.19c818"]]},{"id":"a34961c7.b9cb78","type":"debug","z":"b4370c04.a6c7d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1210,"y":2680,"wires":[]},{"id":"86da049b.8aed18","type":"debug","z":"b4370c04.a6c7d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":2640,"wires":[]},{"id":"7da9517d.305a1","type":"ui_group","name":"","tab":"1ea6ab39.e574b5","order":1,"disp":true,"width":"6","collapse":false},{"id":"1ea6ab39.e574b5","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

When I fill the following in 'change2' i get a list op ball types:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5a81ea540b5573d0ff902c7c39f129f395095ea.png)

But I want to use msg.test to fill in the word 'ball'. So the dropdown 'type' becomes dynamic. So if I choose another product, the types of that product can be chosen  
I tried with `$$.test` to select the string of msg.test but that doesn't work.

I don't understand what I'm doing wrong.

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

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [11 May 2021 06:32 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/6 "2021-05-11T06:32:07Z")

</div>

Can you share the payload containing the differen product types ?

---

<div class="post-metadata">

### Author: ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)
#### Post date: [11 May 2021 06:54 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/7 "2021-05-11T06:54:58Z")

</div>

What do you mean?  
The different product types are in the template node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba458dc470fa0a192efb2dda176e60c09acc3e55.png)

---

<div class="post-metadata">

### Author: ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)
#### Post date: [11 May 2021 07:18 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/8 "2021-05-11T07:18:59Z")

</div>

Hi Wolff,

Maybe you should check out this entry in the forum (_btw_: I received it looking for „depending drop down“):  
[https://discourse.nodered.org/t/problem-with-drop-down-menu/34003](https://discourse.nodered.org/t/problem-with-drop-down-menu/34003)

There you find an option with msg.options to send the possible options for the dropdown; meaning: as soon as one value is selected in the first drop down you can serve the possible options to the second one.

_Update: just saw, that you know the msg.options already. :-/ - Do you definitely need to use the csv or does it maybe make sense to have the values in a function? Are the values updated regularly?_

Cheers  
Ranki

---

<div class="post-metadata">

### Author: ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)
#### Post date: [11 May 2021 07:30 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/9 "2021-05-11T07:30:57Z")

</div>

Hi Ranki,

Thanks for helping!  
Yes I know the msg.options. But I don't send the whole list of options to the second dropdown. Do you tested my flow and saw that?

I need to use the csv to choose the options. Because I want to be able to update the options by updating the csv with no need to update the flow.

What do in need to send to the second dropdown?

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 May 2021 07:42 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/10 "2021-05-11T07:42:56Z")

</div>

Try $lookup()

```auto
[{"id":"965ef7ad.16d87","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"test","v":"product","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":780,"wires":[["d16bafd0.71ee88"]]},{"id":"d16bafd0.71ee88","type":"template","z":"9b3f9f31.c45298","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"product,ball,train,hat\nball,baseball,yellowtrain,bighat\ntrain,football,bluetrain,smallhat\nhat,tennisball,redtrain,mediumhat","output":"str","x":110,"y":860,"wires":[["21a21fc0.7d62"]]},{"id":"21a21fc0.7d62","type":"csv","z":"9b3f9f31.c45298","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"mult","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":260,"y":860,"wires":[["a82f5588.69ba6","639a655b.e7658c"]]},{"id":"a82f5588.69ba6","type":"change","z":"9b3f9f31.c45298","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"$lookup(payload,\"product\")","tot":"jsonata"},{"t":"move","p":"payload","pt":"msg","to":"hold","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":860,"wires":[["e89f457d.42e71"]]},{"id":"639a655b.e7658c","type":"debug","z":"9b3f9f31.c45298","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":360,"y":800,"wires":[]},{"id":"e89f457d.42e71","type":"ui_dropdown","z":"9b3f9f31.c45298","name":"","label":"product","tooltip":"","place":"Select option","group":"7da9517d.305a1","order":4,"width":0,"height":0,"passthru":false,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","x":650,"y":860,"wires":[["a4e8e83.9c24918","1ec6e7b6.c33a7"]]},{"id":"a4e8e83.9c24918","type":"debug","z":"9b3f9f31.c45298","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":800,"y":800,"wires":[]},{"id":"1ec6e7b6.c33a7","type":"change","z":"9b3f9f31.c45298","name":"","rules":[{"t":"set","p":"options","pt":"msg","to":"$lookup($flowContext(\"hold\"),payload)","tot":"jsonata"},{"t":"delete","p":"payload","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":940,"wires":[["48f845ee.cd1b24"]]},{"id":"48f845ee.cd1b24","type":"ui_dropdown","z":"9b3f9f31.c45298","name":"","label":"type","tooltip":"","place":"Select option","group":"7da9517d.305a1","order":4,"width":0,"height":0,"passthru":false,"multiple":false,"options":[{"label":"","value":"","type":"str"}],"payload":"","topic":"topic","topicType":"msg","x":680,"y":940,"wires":[["e9824a3.3b60238"]]},{"id":"e9824a3.3b60238","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":830,"y":940,"wires":[]},{"id":"7da9517d.305a1","type":"ui_group","name":"","tab":"1ea6ab39.e574b5","order":1,"disp":true,"width":"6","collapse":false},{"id":"1ea6ab39.e574b5","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

### Author: ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)
#### Post date: [11 May 2021 09:26 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/11 "2021-05-11T09:26:05Z")

</div>

Thank you very much @E1cid !  
It works like magic. Trying to understand it...

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 May 2021 09:32 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/12 "2021-05-11T09:32:38Z")

</div>

```auto
[{"product":"ball","ball":"baseball","train":"yellowtrain","hat":"bighat"},{"product":"train","ball":"football","train":"bluetrain","hat":"smallhat"},{"product":"hat","ball":"tennisball","train":"redtrain","hat":"mediumhat"}]

```

`$lookup(array_of _object, key_search_value)` will go through each object of the array and find the value of any property with the matching key. It returns an array of values found.

---

<div class="post-metadata">

### Author: ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)
#### Post date: [11 May 2021 09:52 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/13 "2021-05-11T09:52:02Z")

</div>

@MrWolff : I have taken the following input for the jsonata expression.

```auto
{
  "test": "ball",
  "payload": [
    {
      "product": "ball",
      "ball": "baseball",
      "train": "yellowtrain",
      "hat": "bighat"
    },
    {
      "product": "train",
      "ball": "football",
      "train": "bluetrain",
      "hat": "smallhat"
    },
    {
      "product": "hat",
      "ball": "tennisball",
      "train": "redtrain",
      "hat": "mediumhat"
    }
  ]
}

```

For the above input the below jsonata expression will do the job:

```auto
$lookup(payload,test)

```

I have tested it - see following link

- [https://try.jsonata.org/hBTTk0I2u](https://try.jsonata.org/hBTTk0I2u)

For documentation about $lookup() function see:

- [Object functions · JSONata](https://docs.jsonata.org/object-functions)

If the input is in a different format - please share the actual input.

---

<div class="post-metadata">

### Author: ![MrWolff](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrwolff/32/30557_2.png) [@MrWolff](https://discourse.nodered.org/u/MrWolff)
#### Post date: [11 May 2021 12:06 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/14 "2021-05-11T12:06:40Z")

</div>

Thank you very much @janvda !  
That was a good explanation.

---

<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: [25 May 2021 12:07 UTC](https://discourse.nodered.org/t/how-to-use-msg-payload-string-value-in-jsonata/45443/15 "2021-05-25T12:07:34Z")

</div>

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