# Create msg with nested object fields

**URL:** <https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780>\
**Category:** General\
**Created:** [9 October 2022 20:27 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780 "2022-10-09T20:27:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/patrickm/32/68925_2.png) [@patrickm](https://discourse.nodered.org/u/patrickm)\
**Post date:** [9 October 2022 20:27 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780/1 "2022-10-09T20:27:18Z")

</div>

Hi,  
I am new to NodeRed and I'm having trouble with parsing and generating msg. I have a nested object message that I am tried to parse into a new msg. The message is coming from an MQTT In node. In the debug window I see:

Here is the payload from the MQTT IN Node:

```auto
 object
   topic: "sensor/pushnotification"
   payload: "
   {"topic":"sensor.basement_hall.battery","payload":"Battery Low Warning","subtext":"10/9/2022 
   09:55:47 AM","count":null,"percent":null}"
   qos: 2
   retain: false
   _msgid: "7042e909d390b98f"  

```

I run that msg through a JSON node and here is the msg output:

```auto
 object:
   topic: "sensor/pushnotification"
   payload: object
        topic: "sensor.basement_hall.battery"
        payload: "Battery Low Warning"
        subtext: "10/9/2022 09:55:47 AM"
        count: null
        percent: null
  qos: 2
  retain: false
  _msgid: "7042e909d390b98f"

```

I want to generate a new msg with some of the nested object fields and I can't seem to figure out how to reference them in a function. For example :

```auto
 var sTopic = msg.topic.payload.topic;
 var sPayload = msg.topic.payload.payload;
 var sSubtext = msg.topic.payload.subtext;
 msg = {
     payload : sPayload,
     topic : sTopic,
     subtext : sSubtext
 }
 return msg;

```

That results in:  
` function: (error) TypeError: Cannot read properties of undefined (reading 'subtext')`

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 October 2022 20:54 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780/2 "2022-10-09T20:54:42Z")

</div>

> [@patrickm](#):
>
> `msg.topic.payload.subtext;`

Try msg.payload.\< property \> instead

---

<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:** [9 October 2022 20:57 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780/3 "2022-10-09T20:57:16Z")

</div>

> [@patrickm](#):
>
> function: (error) TypeError: Cannot read properties of undefined (reading 'subtext')

Use the tools the editor provides...

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![patrickm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/patrickm/32/68925_2.png) [@patrickm](https://discourse.nodered.org/u/patrickm)\
**Post date:** [9 October 2022 21:35 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780/4 "2022-10-09T21:35:14Z")

</div>

Thanks Steve,  
I tried using the copy path button for the nested payload value and it returned  
`payload.topic`  
which returned  
`sensor/pushnotification`

I replaced it with  
`msg.payload.topic`  
in the function and which resulted in  
`sensor.basement_hall.battery`

Thanks very much for your help  
Patrick

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [9 October 2022 22:00 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780/5 "2022-10-09T22:00:43Z")

</div>

It _might_ simplify things if you set your MQTT-In node to output a parsed JSON object rather than the default autodetect. Possibly you can dispense with the JSON Parser.

---

<div class="post-metadata">

**Author:** ![patrickm](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/patrickm/32/68925_2.png) [@patrickm](https://discourse.nodered.org/u/patrickm)\
**Post date:** [9 October 2022 22:28 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780/6 "2022-10-09T22:28:25Z")

</div>

Yes that got rid of the JSON node. I think I had that set like that initially before my learning curve grief with msg.

---

<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:** [8 December 2022 22:28 UTC](https://discourse.nodered.org/t/create-msg-with-nested-object-fields/68780/7 "2022-12-08T22:28:59Z")

</div>

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