# Node-RED v3.0.2 string to json

**URL:** <https://discourse.nodered.org/t/node-red-v3-0-2-string-to-json/67942>\
**Category:** General\
**Tags:** function-node\
**Created:** [19 September 2022 21:13 UTC](https://discourse.nodered.org/t/node-red-v3-0-2-string-to-json/67942 "2022-09-19T21:13:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mac\_arg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mac_arg/32/67990_2.png) [@mac\_arg](https://discourse.nodered.org/u/mac_arg)\
**Post date:** [19 September 2022 21:13 UTC](https://discourse.nodered.org/t/node-red-v3-0-2-string-to-json/67942/1 "2022-09-19T21:13:58Z")

</div>

I am new to Node Red and I want to parse the data coming in via serial port, and convert it to JSON format.  
The data string I'm getting is from a demo board, and it arrives every one second as follows:

**| Hum[%]: 56.00 | Temp[C]: 21.50 | Pres[hPa]: 966.11 | Temp2[C]: 21.30 | Acc[mg]: 10 -25 1030 | Gyr[mdps]: 2170 -3500 1400 | Acc2[mg]: 27 -26 979 | Mag[mGauss]: -4 163 166 |**

I would like to be able to form a JSON, with the following format:

{  
"hum" : "value",  
"temp" : "value",  
"pres" : "value",  
"acc" : "value",  
"gyr" : "value",  
"pres" : "value",  
"temp\_2" : "value",  
"acc" : "value",  
"gyr" : "value",  
"acc\_2" : "value",  
"mag" : "value",  
}

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [20 September 2022 01:23 UTC](https://discourse.nodered.org/t/node-red-v3-0-2-string-to-json/67942/2 "2022-09-20T01:23:50Z")

</div>

Try this in a function node-

```auto
let text = msg.payload.slice(1, -1) //remove outer |
text = text.split("|") //use | as separator

let data = {
    "hum": "value",
    "temp": "value",
    "pres": "value",
    "temp_2": "value",
    "acc": "value",
    "gyr": "value",
    "acc_2": "value",
    "mag": "value",
}

const key=Object.keys(data) // list of keys

for (let index = 0; index < text.length; index++) { //iterate over text
    let element = text[index].split(":"); // split out the value
    data[key[index]] = element[1].trim() //set required key to value without exta spaces
    }

msg.payload= data
return msg;

```

---

<div class="post-metadata">

**Author:** ![mac\_arg](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mac_arg/32/67990_2.png) [@mac\_arg](https://discourse.nodered.org/u/mac_arg)\
**Post date:** [20 September 2022 16:07 UTC](https://discourse.nodered.org/t/node-red-v3-0-2-string-to-json/67942/3 "2022-09-20T16:07:23Z")

</div>

Thanks for taking the time!  
I will test the code.

Regards!

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [20 September 2022 16:11 UTC](https://discourse.nodered.org/t/node-red-v3-0-2-string-to-json/67942/4 "2022-09-20T16:11:18Z")

</div>

Its based on injectiong this -  
**| Hum[%]: 56.00 | Temp[C]: 21.50 | Pres[hPa]: 966.11 | Temp2[C]: 21.30 | Acc[mg]: 10 -25 1030 | Gyr[mdps]: 2170 -3500 1400 | Acc2[mg]: 27 -26 979 | Mag[mGauss]: -4 163 166 |**

I changed the object to match the payload content as your sample object had some duplicates.

Let me know how you get on with it 😉

---

<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:** [4 October 2022 16:11 UTC](https://discourse.nodered.org/t/node-red-v3-0-2-string-to-json/67942/5 "2022-10-04T16:11:42Z")

</div>

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