Help calculating JSON data lookup

I'm working on a flow using the NHL API.

I am able to get an id number for a player on ice, for example 8474151.

I then want to use that number to lookup the name, which is contained in the same JSON response.

The Player ID (index 0)

returns the id to flow.player0ID

Now, I want to set flow.player0Name based on that returned ID.

The place where this data is stored is

I want to pass along that 7 digit number into that path (number) and then return the data to flow.player0Name.

I created a function node which inserts the number properly.

msg.payload = "payload.gameData.players.ID"+(get.flow("player0ID"))+".fullName"
return msg;

But now I'm at a loss as to how to actually get the data. I try to put msg.payload into a switch node, but I just get my calculated function as a string.

How can I actually look up the data using that calculated string? I'm able to do another API call using the ID, but I'm trying to make it all happen in one.


Here's a screenshot of what I mean by the string being passed through:
Messages Image(1361055785)

Use square bracket notation.


var x = 123456
var s = "ID" + x;
var n = msg.payload.gameData.players[s].fullName

Note- this is not a working example - its for demonstrating how square bracket notation works

Thanks, Steve, much cleaner way of setting up the calculation - though even when using that instead of my calculation above, I'm still returning the full string to my flow variable.



I've moved the data over to a flow variable:

So basically now what I want to be able to do is create some kind of expression that will go to the object based on the ID number and return the "fullName" field. The IDs will be different for each game, so I need to figure out a way to search this object. hmm.

The lookup is not hard in something like JSONata, but I would create an array instead.
Can you post your flow ?

In a function you can get the object playersList from flow and retrieve the item using square bracket notation (ie lookup the id)

Example, add a function node...

var id = msg.payload;
var playersList = flow.get("playersList");
msg.paylaod = playersList[id];
return msg;

... And send the ID of interest into the function & out of it you will get the player object which you can access any part e.g.

msg.payload.fullName and msg.payload.lastName

Ideal for updating several dashboard fields in one go.

Alternatively, if you ONLY want first name, then change the above function code line 3 to msg.paylaod = playersList[id].firstName;

This is exactly what I was looking for, it works perfectly and makes sense! Thanks so much for your help.

1 Like

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