# How do i reference a value out of a JSON string

**URL:** <https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960>\
**Category:** General\
**Created:** [11 January 2021 15:34 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960 "2021-01-11T15:34:35Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [11 January 2021 15:34 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/1 "2021-01-11T15:34:35Z")

</div>

{"Time":"2021-01-11T16:27:50","DS18B20":{"Id":"3C01D6072EAF","Temperature":-19.8},"TempUnit":"C"}  
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.

---

<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:** [11 January 2021 15:35 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/2 "2021-01-11T15:35:43Z")

</div>

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.

[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

> [@DavidMagrobi](#):
>
> I assume i need to use a function, but i have no clue on how to structure the code.

As you seem very new to node-red, I strongly 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:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [11 January 2021 15:44 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/3 "2021-01-11T15:44:43Z")

</div>

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.

---

<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:** [11 January 2021 15:47 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/4 "2021-01-11T15:47:21Z")

</div>

Hi and welcome.

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

or  
pass the string through the json node.

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

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

---

<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:** [11 January 2021 15:47 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/5 "2021-01-11T15:47:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [11 January 2021 16:17 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/6 "2021-01-11T16:17:01Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/abb74d31be667909a528d365e9f4eaf5f3784a48.png)

---

<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:** [11 January 2021 16:23 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/7 "2021-01-11T16:23:40Z")

</div>

Yes your syntax is out.

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

```

will set temp in msg.temp001

```auto
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

 ![Screenshot_20210111-163510](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2eb988015f6426bff8194f47693c2474eef46741.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:** [11 January 2021 16:26 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/9 "2021-01-11T16:26:53Z")

</div>

@DavidMagrobi one of the reasons I aksed you to show us the debug output AND to look at [https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages) 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...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b1136068b2514f663a47ba50a8e05ca8f17dc3c.png)

---

<div class="post-metadata">

**Author:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [11 January 2021 16:35 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/10 "2021-01-11T16:35:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [11 January 2021 16:37 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/11 "2021-01-11T16:37:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [11 January 2021 17:25 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/12 "2021-01-11T17:25:25Z")

</div>

> [@DavidMagrobi](#):
>
> but my inexperience in Java / JSON

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? 😁

---

<div class="post-metadata">

**Author:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [11 January 2021 19:01 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/13 "2021-01-11T19:01:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [11 January 2021 19:28 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/14 "2021-01-11T19:28:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [12 January 2021 05:30 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/15 "2021-01-12T05:30:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![freak23](https://avatars.discourse-cdn.com/v4/letter/f/46a35a/32.png) [@freak23](https://discourse.nodered.org/u/freak23)\
**Post date:** [17 January 2021 06:16 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/16 "2021-01-17T06:16:42Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/c/7cacca9928a47644eb99b9c6bd462d8a91bf33a0.jpeg)

---

<div class="post-metadata">

**Author:** ![DavidMagrobi](https://avatars.discourse-cdn.com/v4/letter/d/258eb7/32.png) [@DavidMagrobi](https://discourse.nodered.org/u/DavidMagrobi)\
**Post date:** [17 January 2021 06:18 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/17 "2021-01-17T06:18:46Z")

</div>

Thanks.  
Works as advertised.

---

<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:** [31 January 2021 06:18 UTC](https://discourse.nodered.org/t/how-do-i-reference-a-value-out-of-a-json-string/38960/18 "2021-01-31T06:18:53Z")

</div>

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