# Extracting a Number from a specific line from a string

**URL:** <https://discourse.nodered.org/t/extracting-a-number-from-a-specific-line-from-a-string/63071>\
**Category:** General\
**Created:** [25 May 2022 20:21 UTC](https://discourse.nodered.org/t/extracting-a-number-from-a-specific-line-from-a-string/63071 "2022-05-25T20:21:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gammalikker](https://avatars.discourse-cdn.com/v4/letter/g/8797f3/32.png) [@gammalikker](https://discourse.nodered.org/u/gammalikker)\
**Post date:** [25 May 2022 20:21 UTC](https://discourse.nodered.org/t/extracting-a-number-from-a-specific-line-from-a-string/63071/1 "2022-05-25T20:21:41Z")

</div>

Hello, i have a "mqtt in" node that receives messages in the from a string from my smart meter. I only need one line from the message or more specifically the last numbers in that line. I have looked around on the forum and other sites and i have found people with similar problems but those solutions have not worked for me.

This is an example of the string:

```auto
{
	"Battery" : 255,
	"RSSI" : 12,
	"description" : "",
	"dtype" : "P1 Smart Meter",
	"hwid" : "12",
	"id" : "1",
	"idx" : 190,
	"name" : "Energy",
	"nvalue" : 0,
	"stype" : "Energy",
	"svalue1" : "34637147",
	"svalue2" : "38439648",
	"svalue3" : "72253",
	"svalue4" : "325054",
	"svalue5" : "1582",
	"svalue6" : "0",
	"unit" : 1
}

```

And this is the line i'm interested in.

```auto
	"svalue3" : "72253",

```

The number can be smaller or larger so [this](https://discourse.nodered.org/t/function-string-extract/6775/5) solution would not work for me. I hope there is someone around that knows of a solution, if i need to provide more info please ask.

---

<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:** [25 May 2022 20:25 UTC](https://discourse.nodered.org/t/extracting-a-number-from-a-specific-line-from-a-string/63071/2 "2022-05-25T20:25:54Z")

</div>

The data are you have posted looks like JSON.

You can grab any value you want.

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

So in your case `msg.payload.svalue3` will hold the value of interest.

If you are new to node-red, I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [25 May 2022 20:30 UTC](https://discourse.nodered.org/t/extracting-a-number-from-a-specific-line-from-a-string/63071/3 "2022-05-25T20:30:14Z")

</div>

If you are using a MQTT-In node (in Node-RED) you could set the output to be a parsed JSON object then just extract the element you want with a function node or a change node.  
 ![parsed](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0f1dad76fe839e344511b8c97fd487de458e9c66.png)

```auto
msg.payload = msg.payload.svalue3;
return msg;

```

---

<div class="post-metadata">

**Author:** ![gammalikker](https://avatars.discourse-cdn.com/v4/letter/g/8797f3/32.png) [@gammalikker](https://discourse.nodered.org/u/gammalikker)\
**Post date:** [25 May 2022 20:33 UTC](https://discourse.nodered.org/t/extracting-a-number-from-a-specific-line-from-a-string/63071/4 "2022-05-25T20:33:53Z")

</div>

Thank you so much! For some reason i put the output of the "mqtt in" node to string instead json after changing that i got the correct output. and together with the suggestion of dynamicdave i got it working just exactly as i was hoping.

---

<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:** [8 June 2022 20:34 UTC](https://discourse.nodered.org/t/extracting-a-number-from-a-specific-line-from-a-string/63071/5 "2022-06-08T20:34:27Z")

</div>

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