# Dynamic string to array

**URL:** <https://discourse.nodered.org/t/dynamic-string-to-array/48136>\
**Category:** General\
**Created:** [7 July 2021 13:25 UTC](https://discourse.nodered.org/t/dynamic-string-to-array/48136 "2021-07-07T13:25:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maraguma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maraguma/32/42793_2.png) [@maraguma](https://discourse.nodered.org/u/maraguma)\
**Post date:** [7 July 2021 13:25 UTC](https://discourse.nodered.org/t/dynamic-string-to-array/48136/1 "2021-07-07T13:25:46Z")

</div>

Hi everyone

I hope someone can help on this. I would like to separate the below string to an array. I tried with two splits and a loop but that did not work as I don't know how to create a single array as an output message. My payload is as follows:

`"Timepoint=44384.47807091435&Temp=998.9&SG=1.004&Beer=Untitled&Color=BLUE:c0:b6:7d:aa:5e:88&Comment="`

The `&` character separates always the the different parameter descriptions and their value. Maybe there is an easier way than using split. The string I get may contain more parameters and values than shown in my example.

If possible I would like to have the number values in the array as numbers and not as string.

At the end I would like to have a message with a payload like:

`payload.timepoint : 44384.47807091435`  
`payload.temp: 998.9`  
`payload.sg : 1.004`  
`and so on...`

I appreciate your help.

---

<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:** [7 July 2021 13:53 UTC](https://discourse.nodered.org/t/dynamic-string-to-array/48136/2 "2021-07-07T13:53:35Z")

</div>

I'm not certain there is a more direct way than splitting and assigning (well apart from using the node-js `URL` but its not included in the function nodes globals by default).

Here is a function that works without additional libs (straight JS)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2bb881b4104c50756a1a4075da1803a1f99143e1.png)

```auto
[{"id":"405b5387f461ec3d","type":"function","z":"f9687f620c582b41","name":"url2object","func":"\nlet dummyURL = msg.payload;\nlet idx = dummyURL.indexOf(\"?\");\nif (idx <= -1) dummyURL = \"?\" + dummyURL;\n\n\nmsg.payload = url2object(dummyURL);\n\nreturn msg;\n\n//adapted from https://stackoverflow.com/questions/8486099/how-do-i-parse-a-url-query-parameters-in-javascript/8486188\nfunction url2object(url) {\n const question = url.indexOf(\"?\");\n let hash = url.indexOf(\"#\");\n if (hash == -1 && question == -1) return {};\n if (hash == -1) hash = url.length;\n const query = question == -1 || hash == question + 1 ? url.substring(hash) :\n url.substring(question + 1, hash);\n const result = {};\n query.split(\"&\").forEach(function (part) {\n if (!part) return;\n part = part.split(\"+\").join(\" \"); // replace every + with space, regexp-free version\n let eq = part.indexOf(\"=\");\n let key = eq > -1 ? part.substr(0, eq) : part;\n let val = eq > -1 ? decodeURIComponent(part.substr(eq + 1)) : \"\";\n let iVal = Number(val);\n if (!isNaN(iVal)) val = iVal;\n const _from = key.indexOf(\"[\");\n if (_from == -1) result[decodeURIComponent(key)] = val;\n else {\n const to = key.indexOf(\"]\", _from);\n const index = decodeURIComponent(key.substring(_from + 1, to));\n key = decodeURIComponent(key.substring(0, _from));\n if (!result[key]) result[key] = [];\n if (!index) {\n result[key].push(val);\n } else {\n result[key][index] = val;\n }\n }\n });\n return result;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2280,"y":820,"wires":[["b87736ab38954bdb"]]},{"id":"58e7ebf25b11fe7e","type":"inject","z":"f9687f620c582b41","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"a=1&b=2&c=3","payloadType":"str","x":2050,"y":900,"wires":[["405b5387f461ec3d"]]},{"id":"b87736ab38954bdb","type":"debug","z":"f9687f620c582b41","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"info","statusType":"auto","x":2290,"y":880,"wires":[]},{"id":"6500dfaabd7b1383","type":"inject","z":"f9687f620c582b41","name":"http://test.com/blah?x=24&y=26&z=26","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"http://test.com/blah?x=24&y=26&z=26","payloadType":"str","x":1970,"y":820,"wires":[["405b5387f461ec3d"]]},{"id":"34a3fc0fe5ef8fc0","type":"inject","z":"f9687f620c582b41","name":"?name=ted&rel=friend&age=21","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"?name=ted&rel=friend&age=21","payloadType":"str","x":1990,"y":860,"wires":[["405b5387f461ec3d"]]}]

```

---

<div class="post-metadata">

**Author:** ![maraguma](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maraguma/32/42793_2.png) [@maraguma](https://discourse.nodered.org/u/maraguma)\
**Post date:** [7 July 2021 15:20 UTC](https://discourse.nodered.org/t/dynamic-string-to-array/48136/3 "2021-07-07T15:20:43Z")

</div>

Thanks, that did exactly the job I was looking for.

---

<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:** [21 July 2021 15:21 UTC](https://discourse.nodered.org/t/dynamic-string-to-array/48136/4 "2021-07-21T15:21:20Z")

</div>

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