# Json, how can i add the msg.payload object as a variable in JSON

**URL:** https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108
**Category:** General
**Created:** [2 April 2020 16:50 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108 "2020-04-02T16:50:06Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Beefy\_carrot](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@Beefy\_carrot](https://discourse.nodered.org/u/Beefy_carrot)
#### Post date: [2 April 2020 16:50 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108/1 "2020-04-02T16:50:07Z")

</div>

Hi! im new to node red, but im trying to send a payload from a sensor over to a database. this works fine using a text string. But my question is, how can i add the msg.payload variable(that holds the data from the sensor) instead of the data variable with the string Hello World?  
json structure is this (added placeholders for the other variables):

msg.payload = {  
'deviceId':"SensorName",  
'key':"KeyData",  
'protocol':"mqtt",  
'data':'Hello world'  
}  
return msg;

---

<div class="post-metadata">

### Author: ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)
#### Post date: [2 April 2020 17:03 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108/2 "2020-04-02T17:03:46Z")

</div>

let a = "some data";  
msg = {  
payload: a,  
}  
or am I looking at this backwards for what you want? otherwise afelix has a good suggestion

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [2 April 2020 17:04 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108/3 "2020-04-02T17:04:24Z")

</div>

Use a change node and read [Working with Messages](https://nodered.org/docs/user-guide/messages).

---

<div class="post-metadata">

### Author: ![Beefy\_carrot](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@Beefy\_carrot](https://discourse.nodered.org/u/Beefy_carrot)
#### Post date: [2 April 2020 22:39 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108/4 "2020-04-02T22:39:00Z")

</div>

dunno 😛 but i solved it by changing it to a java script object and converting it with the json node

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [2 April 2020 23:04 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108/5 "2020-04-02T23:04:08Z")

</div>

> [@Beefy\_carrot](#):
>
> msg.payload = {  
> 'deviceId':"SensorName",  
> 'key':"KeyData",  
> 'protocol':"mqtt",  
> 'data':'Hello world'  
> }  
> return msg;

Shouldn't that be;

```auto
msg.payload = {
   deviceId: SensorName,
   key: KeyData,
   protocol: mqtt,
   data: 'Hello world'
}
return msg;
```

---

<div class="post-metadata">

### Author: ![Beefy\_carrot](https://avatars.discourse-cdn.com/v4/letter/b/ccd318/32.png) [@Beefy\_carrot](https://discourse.nodered.org/u/Beefy_carrot)
#### Post date: [3 April 2020 12:40 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108/6 "2020-04-03T12:40:35Z")

</div>

yes ! 🙂 exactly what i did, changed 'hello world with msg.payload and used a json node to convert. i guess i tried to turn the js object into json manually, the first time ^^. but thanks for reply

---

<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: [17 April 2020 12:42 UTC](https://discourse.nodered.org/t/json-how-can-i-add-the-msg-payload-object-as-a-variable-in-json/24108/7 "2020-04-17T12:42:08Z")

</div>

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