# Extracting data from a msg object with a number in it's name

**URL:** <https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680>\
**Category:** General\
**Created:** [4 December 2022 18:54 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680 "2022-12-04T18:54:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TadyTheFish](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@TadyTheFish](https://discourse.nodered.org/u/TadyTheFish)\
**Post date:** [4 December 2022 18:54 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/1 "2022-12-04T18:54:42Z")

</div>

Hello  
I'm trying process data coming from the OpenWeatherMap node  
but i'm stuck on one parameter that has a number in it's name and the template complains about formatting.

 ![Nodered_problem](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2af7ffb93537333237e1df2aed90fe7d32825edc.png)

So I want to check if msg.data.snow exists in the message and if it does I want to extract msg.data.snow.1h

I tried with hasOwnProperty("snow") but to me it seems like it works only with msg.payload.  
And I can't use `var placeholder = msg.data.snow.1h;` for getting the value

Thank you

---

<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:** [4 December 2022 19:00 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/2 "2022-12-04T19:00:43Z")

</div>

You can use the built-in tools to save you having to figure this out but the answer is you use square brackets notation.

The tools...

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:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [4 December 2022 19:26 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/3 "2022-12-04T19:26:48Z")

</div>

> [@Steve-Mcl](#):
>
> You can use the built-in tools to save you having to figure this out but the answer is you use **square brackets notation**.

```auto
let placeholder = msg.data.snow["1h"];

```

---

<div class="post-metadata">

**Author:** ![TadyTheFish](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@TadyTheFish](https://discourse.nodered.org/u/TadyTheFish)\
**Post date:** [6 December 2022 22:01 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/4 "2022-12-06T22:01:17Z")

</div>

Thank you both!  
You learn something every day 🙂

---

<div class="post-metadata">

**Author:** ![TadyTheFish](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@TadyTheFish](https://discourse.nodered.org/u/TadyTheFish)\
**Post date:** [9 December 2022 18:45 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/5 "2022-12-09T18:45:35Z")

</div>

So it seems I still have errors. It works but it thows errors.

My node

```auto
if (msg.data.hasOwnProperty("snow") == true){
    flow.set("SnowStore", msg.data.snow["1h"]);
}
else{
    if(msg.topic == ""){
        flow.set("SnowStore", '0');
    }
}

```

Example data:

`{"coord":{"lon":99.9999,"lat":99.9999},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"10n"}],"base":"stations","main":{"temp":275.45,"feels_like":275.45,"temp_min":272.53,"temp_max":283.48,"pressure":1000,"humidity":100,"sea_level":1000,"grnd_level":937},"visibility":298,"wind":{"speed":0.82,"deg":300,"gust":1.22},"rain":{"1h":1.63},"clouds":{"all":100},"dt":1670611004,"sys":{"type":2,"id":2010780,"country":"AA","sunrise":1670567697,"sunset":1670599008},"timezone":9900,"id":9999999,"name":"NameOfTown","cod":999}`

Here the snow object is missing and the debug window thows an error

`TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')`

---

<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 December 2022 20:44 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/6 "2022-12-09T20:44:32Z")

</div>

> [@TadyTheFish](#):
>
> `msg.data.hasOwnProperty`

If msg.data is not an object you will get this error.

You have multiple options by probably this is what you want...

```javascript
if (msg.data && msg.data.snow){
    flow.set("SnowStore", msg.data.snow["1h"]);
} else if(msg.topic == ""){
    flow.set("SnowStore", '0');
}

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [9 December 2022 21:23 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/7 "2022-12-09T21:23:45Z")

</div>

> [@Steve-Mcl](#):
>
> `if (msg.data && msg.data.snow){`

... or even shorter:

```javascript
if (msg.data?.snow){

```

---

<div class="post-metadata">

**Author:** ![TadyTheFish](https://avatars.discourse-cdn.com/v4/letter/t/839c29/32.png) [@TadyTheFish](https://discourse.nodered.org/u/TadyTheFish)\
**Post date:** [9 December 2022 21:36 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/8 "2022-12-09T21:36:27Z")

</div>

It works! Thank you  
how come data isn't an object? It says object in the debug window?

---

<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:** [23 December 2022 21:37 UTC](https://discourse.nodered.org/t/extracting-data-from-a-msg-object-with-a-number-in-its-name/71680/9 "2022-12-23T21:37:09Z")

</div>

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