# Extract value on changing date

**URL:** <https://discourse.nodered.org/t/extract-value-on-changing-date/56040>\
**Category:** General\
**Tags:** http-request\
**Created:** [3 January 2022 14:04 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040 "2022-01-03T14:04:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stevom](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Stevom](https://discourse.nodered.org/u/Stevom)\
**Post date:** [3 January 2022 14:04 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/1 "2022-01-03T14:04:24Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a8fe3ef91da193b5680718502a9989edce414dec.png)  
Hello,

i need to extract the tho date from the json object. The date's always change depending what day it is.  
The data in picture is from the last date's, 2022-01-03 and 2022-01-04.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 January 2022 14:20 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/2 "2022-01-03T14:20:02Z")

</div>

Welcome to the forum @Stevom

For the record that is not a JSON object (JSON is a string representation of an object). That is a javascript object.

Given the data you show, _exactly_ what to you want to get out of it?

---

<div class="post-metadata">

**Author:** ![Stevom](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Stevom](https://discourse.nodered.org/u/Stevom)\
**Post date:** [3 January 2022 14:34 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/3 "2022-01-03T14:34:59Z")

</div>

Hi,  
i want to extract the two values from the two date's under watt\_hours\_day.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/f/af295d0dfcb7d7f9532f11098c1a0156abeada73.png)  
This is some data for today and tomorrow.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [3 January 2022 14:50 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/4 "2022-01-03T14:50:11Z")

</div>

Do you know what dates you expect in the message? If so there is a great page in the docs 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)

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

If you don't know what dates there are then you can use javascript in a function. In particular look at [Object.keys()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys). You can use something like  
`let keys = msg.payload.watt_hours_day`  
which will give you an array of the date strings.

---

<div class="post-metadata">

**Author:** ![Stevom](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Stevom](https://discourse.nodered.org/u/Stevom)\
**Post date:** [4 January 2022 09:04 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/5 "2022-01-04T09:04:02Z")

</div>

Hi ,

Thanks for the help.

The two dates are always the date for today and tomorrow.

---

<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:** [4 January 2022 09:25 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/6 "2022-01-04T09:25:32Z")

</div>

Here is an example using A change node, you could also do it using a function node, You do not say how you want the data presented.

```auto
[{"id":"d09c6bd0.6d9d9","type":"inject","z":"1378ba83.90c825","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"watt_hour_days\":{\"2021-01-04\":1234,\"2021-01-05\":5678}}","payloadType":"json","x":200,"y":340,"wires":[["b515b3a4.437d18"]]},{"id":"b515b3a4.437d18","type":"change","z":"1378ba83.90c825","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$keys($$.payload.watt_hour_days).{\"date\": $, \"value\" :$lookup($$.payload.watt_hour_days, $)}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":340,"wires":[["ee01ad6c.ac18b"]]},{"id":"ee01ad6c.ac18b","type":"debug","z":"1378ba83.90c825","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":340,"wires":[]}]

```

expression

```auto
$keys($$.payload.watt_hour_days).{
   "date": $,
   "value" :$lookup($$.payload.watt_hour_days, $)
}

```

---

<div class="post-metadata">

**Author:** ![Stevom](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Stevom](https://discourse.nodered.org/u/Stevom)\
**Post date:** [4 January 2022 10:23 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/7 "2022-01-04T10:23:50Z")

</div>

Hi E1cid,

thanks look great.

But if i try it in my flow. I get an error.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f0848f18bace34be46584ed0685cdbfe0f1271c8.png)

When i debug the http request it is ok. Butt when i put the change node in, i get the error.

---

<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:** [4 January 2022 10:54 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/8 "2022-01-04T10:54:45Z")

</div>

When you posted your question you failed to posted any usable data(image can not be copied). I therefore created my own for the example. If you look at the property names they are not the same. Edit the expression and add your property names.  
eg. `$$.payload.result.watt_hours_day`

---

<div class="post-metadata">

**Author:** ![Stevom](https://avatars.discourse-cdn.com/v4/letter/s/ec9cab/32.png) [@Stevom](https://discourse.nodered.org/u/Stevom)\
**Post date:** [4 January 2022 11:07 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/9 "2022-01-04T11:07:35Z")

</div>

Thank you very much.

---

<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:** [18 January 2022 11:08 UTC](https://discourse.nodered.org/t/extract-value-on-changing-date/56040/10 "2022-01-18T11:08:35Z")

</div>

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