Wildcard in a Javascript Object

1,903 views
Skip to first unread message

David Dempster

unread,
Sep 8, 2017, 1:54:02 PM9/8/17
to Node-RED
Hi to all,
I'm trying to get a value from a Javascript object that has various levels.

If I use the following in Function Node it works fine.

var data = msg.payload["Time Series (1min)"]["2017-09-08 11:01:00"]["1. open"];
msg.payload = data;
return msg;

What I would like to do is make the Function Node pick up the FIRST entry for the datestamp, like the following.

// var data = msg.payload["Time Series (1min)"][0]["1. open"];

I don't really care what the datestamp is, I just want the first one at the top of the list.

Any help/guidance would be appreciated.

Kind regards from David (Dempster)

Nick O'Leary

unread,
Sep 8, 2017, 2:02:25 PM9/8/17
to Node-RED

Can you share an example of your actual data - to remove any ambiguity?

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/1ec82803-5f48-4d4e-8464-6f4b885b2bc2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Dempster

unread,
Sep 8, 2017, 2:39:37 PM9/8/17
to Node-RED
Here's a screenshot - trust it makes sense.

Thanks in advance for your help.

Rdgs - David
stock_price.png

steve rickus

unread,
Sep 8, 2017, 5:14:46 PM9/8/17
to Node-RED
As usual, I'm looking at this as a puzzle to be solved using JSONata ;*)

Assuming that you may want data from time series of any time period (not just "1 min"), I used a jsonata $sift() function to get whichever field starts with "Time Series". I then spread that object into separate objects and retrieve only the first one (presumably that is the latest timestamp). From that object, use the $sift() function again to grab the field starting with "1." The resulting string should  be what you want => "555.0000"

payload
   
.$sift(function($v,$k) { $k ~> /^Time Series/ }).*
   
.$spread()[0].*
   
.$sift(function($v,$k) { $k ~> /^1\. / }).*

Granted, not nearly as readable as the javascript syntax you were wishing for -- but you should be able to put this expression in a change node, replacing the msg.payload with this result.
--
Steve

Nick O'Leary

unread,
Sep 8, 2017, 5:23:04 PM9/8/17
to Node-RED Mailing List
For the regular JavaScript version:


var timeSeriesData = msg.payload["Time Series (1min)"];

// The properties of a JavaScript object don't have a natural order.
// So we have to:
// 1. get the keys
var timeslots = Object.keys(timeSeriesData);

// 2. sort them to ensure they are in order - the format of the timestamp makes this easy
timeslots.sort();

// 3. grab the last one
var lastTimeSlotKey = timeslots.pop();

// 4. grab the actual data:
msg.payload = timeSeriesData[lastTimeSlotKey]["1. open"];
return msg;



Nick



--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

David Dempster

unread,
Sep 9, 2017, 3:33:36 AM9/9/17
to Node-RED
A big "thank you" to Steve and Nick for their brilliant responses.

At the moment I've implemented Nick's version as I could follow what is going on.
I'm going to work out the syntax of Steve's version later today as it also looks "neat".

Like a lot of things in life, once you've seen the solution - it all makes sense (and you think "why on earth didn't I think of that").

Thanks again for all your help/guidance.

Have a great weekend.

On Friday, September 8, 2017 at 6:54:02 PM UTC+1, David Dempster wrote:
Reply all
Reply to author
Forward
0 new messages