Creating a workflow that Combines the output from 2 different node to get a JSON output with key (output coming from one Node) and its Value (Output coming from different node)

Describe the problem/error/question

I have my first node that gets data from PostgreSQL table. Table has User_id and Access_token.
My Second node is HTTP request, which takes this user_ids and access_token as input to fetch some data (say steps and calories).
Following are the requirements -

  1. I need to get this data per user i.e only where the user_id with active access_token. If the user is not active and if any error then that workflow should not fail and only allow for the fetch data for active users.
  2. After first 2 node, I need an output in json format that has user_id(coming from 1st node Postgresql node) as keys with their respective steps, calories data(coming from 2nd HTTP req node) as values.
    Something like -
    [
    <User_id>:
    {

“Steps”:4489
“caloriesOut” : 120

},
<User_id>:
{

“Steps”:273461,
“caloriesOut” : 160

}

]

Please share your workflow

Http node -

Set node to retrieve only steps data -

Information on your n8n setup

  • n8n version: - Version 0.209.4 (Desktop version)
  • Database (default: SQLite): - Postgresql
  • Operating system: - Windows 10

Hi @SWAPNIL_NANDANWAR, sounds like you need to do two things here:

  1. Merge your PostgreSQL data (including the user ID) with the HTTP Request data
  2. Transform your data in the desired format

For the first step you would want to use the Merge, for the second step the Set node you already use would be the right choice.

Here’s how it could look in a combined workflow:

This would produce the JSON structure you have described:

Having different keys on each item can become a bit difficult to handle in n8n though. So depending on what you have in mind with this dataset you might want to consider writing this result into a single field, for example using the Item Lists node:

Hope this helps!

3 Likes

@MutedJam Thank You so much. This worked for me.
I would also like to request you, if you could suggest a solution for my first requirement.
Here, If the access token is expired/invalid, that will throw an error. Hence, I need to continue the above process for active access token user(that will not have any error) and also want to keep track of such user_ids whose access tokens are expired(due to which I might receive and authentication error).

1 Like

If the access token is expired/invalid, that will throw an error

I take it the error would be thrown on your HTTP Request node? You probably want to enable the “Continue On Fail” option of the node like so:

image

This way, your workflow won’t stop when encountering an error. You can then use an IF node at the end of your workflow to check whether an error is present and store it as needed.

1 Like

@MutedJam , Thank You for all information provided. It helped me to run most of the requirement as expected.
However, as per the second requirement wherein I wanted to keep track of such user_id that gets error due to any reason( like access token expired or wrong user_id or wrong access token etc). I want to keep track of this errors as well as corresponding user_id for which error occurred( Result should be same as our success scenario)
I tried creating the the flow with if node and then merge node but the user_ids against the error are not matching. It seems that Under Merge Node using option “Merge by position” does not guarantee the relationship of data(i.e user_id to its output).
Please check the below flow and internal insights of nodes.

Workflow -

If Node -

Merge Node - Here it has wrongly associated the user_id with error. User_id should have been Test123.

Could you please suggest how to handle this?
I really appreciate for all the support and information provided.

1 Like

Hi @SWAPNIL_NANDANWAR, looks like you are merging by position here, but the order of items might not match (so the first item on input 1 might not correspond to the first item item on your input 2).

So you probably want to use another Combination Mode on your Merge node here. Merge by Fields should work, but it would require that both inputs have a field specifying the the same id. So you might need to change your data format a bit.

@MutedJam , I thought of same solution, but the issue here is the response from HTTP Request. This node output(be it successful user_id execution or User_id with error) will not have user_ids. Hence how would we even combine them by ids that is a tricky part.
Could you please suggest on this?

the issue here is the response from HTTP Request. This node output(be it successful user_id execution or User_id with error) will not have user_ids

Hi @SWAPNIL_NANDANWAR, you could add a Set node after your HTTP Request node in order to add a field with the user ID from before your HTTP Request node. @giulioandreini has shown the idea over here for a different node, but it should work just as well with the HTTP Request one :slight_smile:

Hi @MutedJam , I have tried below flow wherein I am trying to iterate through each row(By using Function node with mode = Run Once for each Item) and get the simultaneous output and then combine it. While I execute the nodes individually(specially the final node i.e SET node), it executes successfully but when I execute the whole workflow then the final set node is giving me error. Also I want both the set node to execute as One has successful HTTP response without error and Second has error related data.

  1. When Whole workflow executed -

  1. When Executed individually -
    This runs successfully with desired outputs
    a. Set Node for collecting error related info -

b. Set Node for collecting important info like Steps -

I need both the branches of IF node output to run. This is how I would be able to get the required output.

While I execute the nodes individually(specially the final node i.e SET node), it executes successfully but when I execute the whole workflow then the final set node is giving me error

Hi @SWAPNIL_NANDANWAR, I am so sorry you are having trouble. Unfortunately you only shared screenshots, but not a workflow using which I could try to reproduce your problem. Seeing the desktop app is a bit outdated, I wonder if this could be a problem with the n8n version you currently have in use.

Perhaps you can start the current latest version of n8n using docker and verify if the problem persists? Assuming you have docker desktop installed, these two commands in the terminal should do the job (replace SWAPNIL_NANDANWAR with your actual Windows username):

docker pull docker.n8n.io/n8nio/n8n
docker run -it --rm --name n8n -p 5678:5678 -v C:\Users\SWAPNIL_NANDANWAR\.n8n:/home/node/.n8n docker.n8n.io/n8nio/n8n

If the problem persists, can you share a workflow using which I can reproduce it? Simply select your workflow on the canvas, press Ctrl+C to copy it and then use Ctrl+V here on the forum to paste it.

Hi @MutedJam , I have figured out the way to get the whole thing working. Now its working as expected. I am pasting the screenshot of this -

2 Likes

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