# Referrencing msg.payload in JSON

**URL:** <https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986>\
**Category:** General\
**Created:** [21 June 2018 14:45 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986 "2018-06-21T14:45:01Z")\
**Posts on this page:** 13\
**Page:** 2

<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:** [9 December 2018 11:44 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/21 "2018-12-09T11:44:52Z")

</div>

Show us the complete text you are entering please.

---

<div class="post-metadata">

**Author:** ![ioclaudio](https://avatars.discourse-cdn.com/v4/letter/i/c67d28/32.png) [@ioclaudio](https://discourse.nodered.org/u/ioclaudio)\
**Post date:** [9 December 2018 11:55 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/22 "2018-12-09T11:55:31Z")

</div>

Ok, excuse me, I've solved.  
The right syntax was`$.topic`.  
But I had to choose " **expression**" and not " **json**" in the `to` field.  
Thank you.

cld

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [10 December 2018 03:43 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/23 "2018-12-10T03:43:26Z")

</div>

> [@ioclaudio](#):
>
> Ok, excuse me, I've solved.

We would still like to see the complete question in context because a lot of people, including me, learn from this forum.

---

<div class="post-metadata">

**Author:** ![ioclaudio](https://avatars.discourse-cdn.com/v4/letter/i/c67d28/32.png) [@ioclaudio](https://discourse.nodered.org/u/ioclaudio)\
**Post date:** [10 December 2018 07:27 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/24 "2018-12-10T07:27:04Z")

</div>

Ok, this is the wrong node:  
`[{"id":"98b6dbec.1356f8","type":"change","z":"d960624e.e361d","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":220,"wires":[[]]}]`

this is the good one:  
`[{"id":"eff8325f.f473f","type":"change","z":"d960624e.e361d","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":100,"wires":[[]]}]`

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [10 December 2018 14:01 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/25 "2018-12-10T14:01:16Z")

</div>

> [@ioclaudio](#):
>
> [{"id":"eff8325f.f473f","type":"change","z":"d960624e.e361d","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t "\_id": $.topic,\t "screen": "off"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":100,"wires":[]}]

They look the same to me?

Bad:

```
{
 "_id": $.topic,
 "screen": "off"
}

```

Good:

```
{
 "_id": $.topic,
 "screen": "off"
}

```

---

<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:** [10 December 2018 14:22 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/26 "2018-12-10T14:22:09Z")

</div>

@SteveMann the key difference is the _type_ of the field. If you select JSON type, then you cannot do any sort of referencing of message properties - it is entirely static. If you select the Expression type then you can use JSONata expressions that reference message properties.

---

<div class="post-metadata">

**Author:** ![SteveMann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevemann/32/3810_2.png) [@SteveMann](https://discourse.nodered.org/u/SteveMann)\
**Post date:** [10 December 2018 17:03 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/27 "2018-12-10T17:03:13Z")

</div>

> [@ioclaudio](#):
>
> [{"id":"eff8325f.f473f","type":"change","z":"d960624e.e361d","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t "\_id": $.topic,\t "screen": "off"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":100,"wires":[]}]

You already know that JSON formatting is my Achilles heel, but after importing the two examples, I see no difference between them. What am I missing?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [10 December 2018 17:15 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/28 "2018-12-10T17:15:57Z")

</div>

@SteveMann I agree with you, they are both using an expression and both expressions are identical.

---

<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:** [10 December 2018 17:33 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/29 "2018-12-10T17:33:28Z")

</div>

Yup, on inspection the nodes are identical. I'll give @ioclaudio the benefit of the doubt and put that down to copy/paste error given he's acknowledged he needed to use the Expression type rather than JSON.

---

<div class="post-metadata">

**Author:** ![ioclaudio](https://avatars.discourse-cdn.com/v4/letter/i/c67d28/32.png) [@ioclaudio](https://discourse.nodered.org/u/ioclaudio)\
**Post date:** [10 December 2018 19:12 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/30 "2018-12-10T19:12:13Z")

</div>

Yes, my error was to use the wrong type for the field.  
I used `Json` instead of `Jsonata`.

Wrong:  
`[{"id":"94f7be69.564c3","type":"change","z":"1802dedb.cf8d01","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[[]]}]`

Right:  
`[{"id":"94f7be69.564c3","type":"change","z":"1802dedb.cf8d01","name":"create off","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t \"_id\": $.topic,\t \"screen\": \"off\"\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[[]]}]`

cld

---

<div class="post-metadata">

**Author:** ![Zbrooklyn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zbrooklyn/32/34446_2.png) [@Zbrooklyn](https://discourse.nodered.org/u/Zbrooklyn)\
**Post date:** [30 December 2020 00:58 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/31 "2020-12-30T00:58:40Z")

</div>

I need to pull a global variable using JSON how do I do that  
@zenofmud @knolleary

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [30 December 2020 01:28 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/32 "2020-12-30T01:28:28Z")

</div>

@Zbrooklyn - seeing how this thread is two years old, it would be a good idea if you started a new thread and explained what you are trying to do and what you have done so far.

With that, I'm going to close this thread.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [30 December 2020 01:28 UTC](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986/33 "2020-12-30T01:28:34Z")

</div>

Closed on 30 December 2020 01:28 UTC



[Previous page](https://discourse.nodered.org/t/referrencing-msg-payload-in-json/986.md?page=1)
