Dynamic string to array

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.

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)

[{"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"]]}]

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

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