# Extracting specific data from string

**URL:** <https://discourse.nodered.org/t/extracting-specific-data-from-string/41073>\
**Category:** General\
**Created:** [16 February 2021 09:25 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073 "2021-02-16T09:25:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jvalour](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jvalour](https://discourse.nodered.org/u/Jvalour)\
**Post date:** [16 February 2021 09:25 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073/1 "2021-02-16T09:25:21Z")

</div>

I receive a string of data from my sensor via MQTT but I do not know how to retrieve a specific variable (e.g. temperature) from the string.

From the debug window:  
msg.payload : string[323]  
{'data\_format': 10, 'humidity': 66.0, 'temperature': 21.6, 'pressure': 1002.83, 'acceleration\_total': 0.005, 'acceleration\_x': 0.003, 'acceleration\_y': 0.004, 'acceleration\_z': 0.0, 'v\_bat': 3.199, 'v\_cap': 0.0, 'rspi': 0.0, 'mic': 46, 'freq': 1.0, 'counter': 228, 'dropPackets': 194, 'time': '2021-02-16T08:22:22:866734Z'}

I often use the JSON converter and simply set my message to msg.payload.temperature and I will be able to get the temperature data. However, now there is an error when I use the converter or the payload is undefined without the JSON converter. Please help!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c267ab93679eaa30095cb0ee623909a7a26fae4a.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/64ba155aa418bfe0a5525c96a3765d2f8663d739.png)

---

<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:** [16 February 2021 09:28 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073/2 "2021-02-16T09:28:26Z")

</div>

That JSON is invalid - should be double quotes around property names (there are many online JSON validators you can test it on)

The question is, are you in control of the data being sent? If so, change all the single quotes to double quotes and it WILL be valid JSON.

If not, then you will need to hack it (search and replace all single quotes for double quotes)

---

<div class="post-metadata">

**Author:** ![paulkeates](https://avatars.discourse-cdn.com/v4/letter/p/9de053/32.png) [@paulkeates](https://discourse.nodered.org/u/paulkeates)\
**Post date:** [16 February 2021 20:09 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073/3 "2021-02-16T20:09:39Z")

</div>

Hi,

You can use the 'string' node (node-red-contrib-string) to change from single quote to double quote.

Cheers,

Paul

---

<div class="post-metadata">

**Author:** ![Jvalour](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jvalour](https://discourse.nodered.org/u/Jvalour)\
**Post date:** [19 February 2021 07:25 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073/4 "2021-02-19T07:25:31Z")

</div>

Yeah got it, thanks!

---

<div class="post-metadata">

**Author:** ![Jvalour](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jvalour](https://discourse.nodered.org/u/Jvalour)\
**Post date:** [19 February 2021 07:26 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073/5 "2021-02-19T07:26:01Z")

</div>

The 'string' node is very useful. Thanks!

---

<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:** [19 February 2021 09:07 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073/6 "2021-02-19T09:07:09Z")

</div>

Changing the string at the destination is a hack. Fixing the JSON at source is the better option.

PS, the string node is a waste of space, a function node and a one-liner (or even a change node) could easily have hacked up the string into valid JSON - i didn't mention it because it wasn't the "right thing to do"

---

<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:** [5 March 2021 09:07 UTC](https://discourse.nodered.org/t/extracting-specific-data-from-string/41073/7 "2021-03-05T09:07:46Z")

</div>

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