# Transform JSON field to string array

**URL:** <https://discourse.nodered.org/t/transform-json-field-to-string-array/14163>\
**Category:** General\
**Created:** [9 August 2019 08:36 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163 "2019-08-09T08:36:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![carlosglez](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@carlosglez](https://discourse.nodered.org/u/carlosglez)\
**Post date:** [9 August 2019 08:36 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/1 "2019-08-09T08:36:42Z")

</div>

Hello,

I am working with the node "node-red-contrib-modbustcp" and I am trying to create a server. I know that the server is working because I am connected to it by using a modbus master. The problem is that I need to transform the field payload that you can see remarked in red in the image attached into a string array and I don't know how to get it.

 ![modbus](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/591c322aac4d0152381d091f9e5cde2d532c0844.png)

I want only the numbers that are remarked, not the name. You know, something like: "(62296,1743,18368,5015,61975,24713,35923,16468)"

Thanks!

---

<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 August 2019 08:42 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/2 "2019-08-09T08:42:17Z")

</div>

Can you post the debug output in text ?  
Or even better, make a mockup flow that only produces the output.

Come to think of it, you can use the csv node to make a comma separated string.

---

<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 August 2019 09:00 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/3 "2019-08-09T09:00:09Z")

</div>

Are you sure you want as a string with embedded values, not as an array for example?  
Whichever, first have a look at the [article on working with messages](https://nodered.org/docs/user-guide/messages) in the docs, which will show you how to access the components. Then if you do really want it as a string as you have shown then you can use the string interpolation technique to build the string in a function node, something like this

```auto
msg.payload = `(${msg.payload.BATTERIA},${msg.payload.somethingelse}, ...)`

```

---

<div class="post-metadata">

**Author:** ![carlosglez](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@carlosglez](https://discourse.nodered.org/u/carlosglez)\
**Post date:** [9 August 2019 09:29 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/4 "2019-08-09T09:29:40Z")

</div>

The Modbus Server says this: " **msg.payload** must be an array of numbers or strings with values between 0:65535". I understand that I need an array but that its composed by strings.

> [@Colin](#):
>
> msg.payload = `(${msg.payload.BATTERIA},${msg.payload.somethingelse}, ...)`

So, what do I put in the function node exactly?

---

<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 August 2019 10:01 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/5 "2019-08-09T10:01:48Z")

</div>

So you want an array of values, not a string containing the values. So in the function node you will need something like

```auto
msg.payload = [msg.payload.BATTERIA, msg.payload.somethingelse, ...]
return msg

```

The square brackets tell it to construct an array containing the values.  
There are other ways rather than using a function node, but for learning a bit about javascript that is a good way.

---

<div class="post-metadata">

**Author:** ![carlosglez](https://avatars.discourse-cdn.com/v4/letter/c/4af34b/32.png) [@carlosglez](https://discourse.nodered.org/u/carlosglez)\
**Post date:** [9 August 2019 10:04 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/6 "2019-08-09T10:04:41Z")

</div>

ok, thank you! That is exactly what I am looking for.

Regards!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 August 2019 13:44 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/7 "2019-08-09T13:44:38Z")

</div>

Simpler still, use `Object.values(msg.payload)`.

> **[Object.values()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values)**
>
> The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

If that is all you want to do in the function, I think you can do that with JSONata as well.

---

<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 August 2019 14:03 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/8 "2019-08-09T14:03:56Z")

</div>

> [@TotallyInformation](#):
>
> `Object.values(msg.payload)`

I assume that @carlosglez needs them in some specific order, likely not the same as Object.values() will return.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [9 August 2019 17:24 UTC](https://discourse.nodered.org/t/transform-json-field-to-string-array/14163/9 "2019-08-09T17:24:32Z")

</div>

> [@Colin](#):
>
> I assume that @carlosglez needs them in some specific order, likely not the same as Object.values() will return.

I _think_ that it returns them in the order found but I might be wrong.
