# Convert string of values to key/value object

**URL:** <https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676>\
**Category:** General\
**Created:** [29 September 2021 15:50 UTC](https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676 "2021-09-29T15:50:51Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [29 September 2021 16:06 UTC](https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676/2 "2021-09-29T16:06:57Z")

</div>

There are a few examples of this around the forum.

For example, you could easily adapt this: [Help converting string to key/value object - #2 by Steve-Mcl](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/2)

E.g...

```auto
const lines = msg.payload.split("\n");
let result = {};

for (let i = 0; i < lines.length ; i++) {
    const line = lines[i].trim();
    if(!line) continue;
    let parts = line.split(":");
    if(parts.length != 2) continue;
    let key = parts[0].trim();
    let val = parts[1].trim();
    if(isNumeric(val)) val = Number(val)
    result[key] = val;
}

msg.payload = result;
return msg;

function isNumeric(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

```

☝ Untested - may need tweaking

---

_[View the full topic](https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676)._
