How do i reference a value out of a JSON string

I need only the temperature which in this case is -19.8, this value will go on to be used in various other flows
I assume i need to use a function, but i have no clue on how to structure the code.

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

As you seem very new to node-red, I strongly recommend watching this playlist: Node-RED Essentials. 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.

Correct, day 2 on Node-Red.
I have gone over many clips, but not seeming to get the basics correct. Suffering from a information overload.

Hi and welcome.

Either have the node that receives the string, return a js object, which the mqtt node and http nodes do.

pass the string through the json node.

in function node
msg.payload = JSON.parse(msg.payload);

then access the value with msg.payload.DS18B20.Temperature

Show me a screenshot of the debug output containing that message and a screenshot of your flow.

I have set the Mqtt incoming as a JSON object, i then send this into a Function, assuming this is correct, this is where i am getting lost, pretty sure it is an error in my syntax

Yes your syntax is out.

msg.temp001 = msg.payload.DS18B20.Temperature;
return msg;

will set temp in msg.temp001

msg.payload.temp = msg.payload.DS18B20.Temperature;
return msg;

will set temp in msg.payload.temp

It all depends where you want the temp of course.

[edit] you can also set debu nodes to complete message object as below

@DavidMagrobi one of the reasons I aksed you to show us the debug output AND to look at was so that you can fix this type of thing for yourself. You learn nothing being handed a solution.

I will point it out for you...

1 Like

I really do appreciate the sentiment, i have been banging my head against this for the better part of 12 hours, i have used the example you reference but my inexperience in Java / JSON is preventing me from seeing the error in my syntax. I was able to send the value directly into the dashboard "gauge" using {{payload.DS18B20.Temperature}} so i know i am on the right track.
I have been using the Debug extensively and absolutely love the function from a fault-finding point of view.

1 Like

Thanks i will give that a try and see if it gives me any further insights

Just for reference, it's JavaScript not 'Java'

Give it some time and it will start to sink in. After all, when you first went to school, did they start you with calculus? :grin:

Got it running, using a change node.
Still would like to know if it could be done using a Function node, as this was the direction i was pointed by others.

Can you share how exactly you configured the Change node?

It definitely will be possible to do so in a function node, but if all your doing is setting the message property, then the Change node is the right node to use.

I will send the config on the change node when I am able.
The important thing for me is your type of feedback, as I am new to Node-Red i need to make sure I learn and use sensable logic and routing in the flows, this is important as my skill on Node-Red grows so will the complexity of the projects.

Hi David,

an easy way to extract one value out of a json string is to use a change node and set the following for your need:

Works as advertised.

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