# 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:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChristophCaina](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christophcaina/32/37783_2.png) [@ChristophCaina](https://discourse.nodered.org/u/ChristophCaina)\
**Post date:** [29 September 2021 15:50 UTC](https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676/1 "2021-09-29T15:50:51Z")

</div>

Hi,  
I am reading the SMART values from a Harddrive and getting the following payload back:

```auto
{
  "health":"PASSED",
  "type":"text",
  "wearout":90,
  "text":"\n
     SMART/Health Information (NVMe Log 0x02)
	 \n
	 Critical Warning: 0x00
	 \n
	 Temperature: 42 Celsius
	 \n
	 Available Spare: 100%
	 \n
	 Available Spare Threshold: 100%
	 \n
	 Percentage Used: 10%
	 \n
	 Data Units Read: 43,125,108 [22.0 TB]
	 \n
	 Data Units Written: 16,184,137 [8.28 TB]
	 \n
	 Host Read Commands: 636,777,953
	 \n
	 Host Write Commands: 803,393,168
	 \n
	 Controller Busy Time: 4,597
	 \n
	 Power Cycles: 89
	 \n
	 Power On Hours: 15,157
	 \n
	 Unsafe Shutdowns: 37
	 \n
	 Media and Data Integrity Errors: 0
	 \n
	 Error Information Log Entries: 0
	 \n
	 Warning Comp. Temperature Time: 0
	 \n
	 Critical Comp. Temperature Time: 0
	 \n
	 Temperature Sensor 2: 42 Celsius
	 \n
	 \n"
}

```

Now, I want to work with the "text" and either create a new JSON output from it, like:

```auto
{
  "SMART/Health Information (NVMe Log 0x02)":
  {
    "Critical Warning":0x00,
    "Temperature":"42 Celsius", //maybe this can be modified to 42 (without 'celsius')
    "Available Spare":"100%", //maybe this can be modified to 100 without percentage
    "Available Spare Threshold":"100%", //maybe this can be modified to 100 without percentage
    "Percentage Used":"10%", //maybe this can be modified to 100 without percentage
    "Data Units Read":"43,125,108 [22.0 TB]",
    "Data Units Written":"16,184,137 [8.28 TB]",
    "Host Read Commands":"636,777,953",
    "Host Write Commands":"803,393,168",
    "Controller Busy Time":"4,597",
    "Power Cycles":89,
    "Power On Hours":"15,157",
    "Unsafe Shutdowns":37,
    "Media and Data Integrity Errors":0,
    "Error Information Log Entries":0,
    "Warning Comp. Temperature Time":0,
    "Critical Comp. Temperature Time":0,
    "Temperature Sensor 2":"42 Celsius" //maybe this can be modified to 42 (without 'celsius')
  }
}

```

Thanks and with best regards,  
Christoph

---

<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

---

<div class="post-metadata">

**Author:** ![ChristophCaina](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christophcaina/32/37783_2.png) [@ChristophCaina](https://discourse.nodered.org/u/ChristophCaina)\
**Post date:** [29 September 2021 16:09 UTC](https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676/3 "2021-09-29T16:09:43Z")

</div>

thanks, I will test it with this and reply a feedback

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 September 2021 17:09 UTC](https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676/4 "2021-09-29T17:09:29Z")

</div>

This should remove % and Celsius from the result

```auto
const lines = msg.payload.text.split(/\n/).filter(e => e.trim() !== "");
let obj = lines[0].trim();
let output = {[obj]:{}};
for (i = 1; i < lines.length; i++){
      let parts = lines[i].split(":");
      let key = parts[0].trim();
      let bit = parts[1].split(/%|Celsius/)[0].trim()
      if (!isNaN(bit)){
          bit = Number(bit)
      }
      output[obj][key] = bit;
}
msg.payload = output;
return msg;

```

---

<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:** [28 November 2021 17:09 UTC](https://discourse.nodered.org/t/convert-string-of-values-to-key-value-object/51676/5 "2021-11-28T17:09:45Z")

</div>

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