# Help converting string to key/value object

**URL:** <https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636>\
**Category:** General\
**Created:** [24 January 2021 09:44 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636 "2021-01-24T09:44:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amishra123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amishra123/32/35823_2.png) [@amishra123](https://discourse.nodered.org/u/amishra123)\
**Post date:** [24 January 2021 09:44 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/1 "2021-01-24T09:44:48Z")

</div>

Hope everyone is well! Was hoping you may be able to help me with this or point me in the right direction!  
I'm trying to extract data from my emfit QS.  
I can HTTP GET request it and I get this back in the payload as a string

```auto
payload: string
SER=001803<br>
TS=1611481080<br>
TS_R=91663<br>
PRES=1<br>
HR=0<br>
HR_DM=0<br>
RR=0.0<br>
RR_DM=0<br>
ACT=4<br>
ACT_DM=0<br>
FW=t120 v2.2.1
<br>
END=1<br>

```

I would ideally like to extract these all into key pairs to feed into my home assistant. Maybe even msg.PRES, msg.SER etc. I've tried playing around with the split node and it kind of works but keeps them all as strings still.

Any help would be greatly appreciated 🙂

---

<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:** [24 January 2021 09:59 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/2 "2021-01-24T09:59:49Z")

</div>

string split is probably best bet.

Split on `<br>` then loop the array and split on `=`

e.g...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0fbdb74f230076ef6fd347b44d54800dfa4a0758.png)

```auto
const lines = msg.payload.split("<br>");
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);
}

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [24 January 2021 10:21 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/3 "2021-01-24T10:21:52Z")

</div>

(though SER is probably the serial number so should remain as a string )

---

<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:** [24 January 2021 10:25 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/4 "2021-01-24T10:25:14Z")

</div>

Ah, missed the leading zeros.

Revised...

```auto
const lines = msg.payload.split("<br>");
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(key != "SER" && isNumeric(val)) val = Number(val)
    result[key] = val;
}

msg.payload = result;
return msg;

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

```

---

<div class="post-metadata">

**Author:** ![amishra123](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/amishra123/32/35823_2.png) [@amishra123](https://discourse.nodered.org/u/amishra123)\
**Post date:** [24 January 2021 11:31 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/5 "2021-01-24T11:31:18Z")

</div>

That works perfectly, thank you so much!! World needs more people like you!!  
I will spend some time learning/understanding your code as well, it is much appreciated!!  
I can now connect multiple sensor output nodes to that with the each of the different paths of the key/pairs.  
Thank you again for your rapid response!

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [26 January 2021 21:25 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/6 "2021-01-26T21:25:59Z")

</div>

In Node-RED, there are always many ways to restructure your data. My favorite is to use a JSONata expression inside a `change` node, like so:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/e/ce7cd4f946335ec28b0fbefa23d240c12051a7b6.png)

Using the Expression Test tab with your data, you can see the results in real-time as you type the expression in the window above -- very nice!

Although the expressions are very powerful, the lambda syntax of the language itself can be a bit puzzling to read... Here is a short description of what this expression does:

```auto
payload.$split('<br>').[$split('=')] {
    $[0]: $[0] in ['SER', 'FW'] ? $[1]
        : $[0] = 'TS' ? $fromMillis($number($[1])*1000)
        : $number($[1])
}

```

- **Line 1:** split the payload into an array of "key=value" strings, and then split each of those strings into a 2-element array of `["key", "value"]` pairs
- **Lines 1 - 5:** from the opening `{` to closing `}` is the body of a single output object, with:
- **Line 2:** field name pulled from the first pair element `$[0]`, with 'SER' and 'FW' field values being the string in the second pair element `$[1]`
- **Line 3:** if the field name is 'TS' then multiply the value of secs. by 1000 to get millis, and convert to a datetime value
- **Line 4:** for all other field names, just get the numeric value of the string

Most people would stick with plain Javascript for maintainability, but I just wanted to show an alternative, which I think is worth learning. Hope this helps more than it confuses!

---

<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:** [9 February 2021 21:27 UTC](https://discourse.nodered.org/t/help-converting-string-to-key-value-object/39636/7 "2021-02-09T21:27:00Z")

</div>

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