# How to change strings into integer from an object

**URL:** <https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266>\
**Category:** General\
**Created:** [19 October 2022 14:15 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266 "2022-10-19T14:15:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ulrich](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ulrich/32/69312_2.png) [@Ulrich](https://discourse.nodered.org/u/Ulrich)\
**Post date:** [19 October 2022 14:15 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/1 "2022-10-19T14:15:48Z")

</div>

This is my msg.payload Object:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1edf4a06c97f787a69d64a7fe749083c55b43c60.png)

How could i change all string values into integers?

---

<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:** [19 October 2022 14:27 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/2 "2022-10-19T14:27:13Z")

</div>

I think a function like this will work...

```auto
msg.payload = {"temperature":"35.1", "humidity":"27.3", "location": "Las Vegas"}
for (const key in msg.payload) {
    if (parseInt(msg.payload[key])) {
        msg.payload[key] = parseInt(msg.payload[key]);
    }  
}
return msg;

```

But I see that your example NewATURVendor is almost numeric (Hex?) and you might want to perform parseInt() only on selected fields.

---

<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:** [19 October 2022 17:41 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/3 "2022-10-19T17:41:01Z")

</div>

> [@jbudd](#):
>
> `if (parseInt(msg.payload[key]))`

I think that may need to be a bit more sophisticated. If the string value is "0" then the `if` will fail. Perhaps it should be a NaN test.

---

<div class="post-metadata">

**Author:** ![Ulrich](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ulrich/32/69312_2.png) [@Ulrich](https://discourse.nodered.org/u/Ulrich)\
**Post date:** [20 October 2022 10:25 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/4 "2022-10-20T10:25:17Z")

</div>

Thanks a lot. Here is my complete solution:

```auto
msg.measurement = "ZOOMsoft/DSL"

for ( const key in msg.payload ) {
    if ( typeof msg.payload[key] == "undefined" ) {
        msg.payload[key] = 0;
        node.warn(key);

    } else {
        if ( parseInt( msg.payload[key] ) ) {
            msg.payload[key] = parseInt( msg.payload[key] );
        }
    
    }

}

msg.payload = [msg.payload , { name: "Banu Haus" }];

return msg;

```

after that i send the data to an influxdb

---

<div class="post-metadata">

**Author:** ![Ulrich](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ulrich/32/69312_2.png) [@Ulrich](https://discourse.nodered.org/u/Ulrich)\
**Post date:** [20 October 2022 15:51 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/5 "2022-10-20T15:51:43Z")

</div>

But sometimes i get errors:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/4791b23839c496f2417c4ca7be0a4592bcb76f52.png)

If typeof msg.payload[key] == "undefined" is not correct?

---

<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:** [20 October 2022 16:15 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/6 "2022-10-20T16:15:44Z")

</div>

Add two named debug node, showing what is going into the function and what is coming out. What do they show when the write fails?

---

<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:** [20 October 2022 16:21 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/7 "2022-10-20T16:21:21Z")

</div>

`Period=undefined`

That might be the spot where it fails.

---

<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:** [19 December 2022 16:21 UTC](https://discourse.nodered.org/t/how-to-change-strings-into-integer-from-an-object/69266/8 "2022-12-19T16:21:58Z")

</div>

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