Multiple input types in function code

Describe the problem/error/question

I am trying to configure a Function/Code node that takes two inputs of different type.
The first input is the result of an API call which contains a lot of IDs which aren’t understandable easily. I want to “convert” these IDs into actual texts. The IDs aren’t only as values of the JSON but as headers as well.
The second input is a mapping JSON between the IDs and their corresponding texts.

A simple function/code node should be able to do the mapping correctly, converting the IDs found in the first JSON into actual texts, and outputting a new, understandable and clean JSON.

I’ve connected the two inputs (mapping function & API result) into one function node, however that function will be applied to the two inputs & thus output two different JSON, which is not what I want.

I have tried a “Merge” node, however this merges datasets/JSONs that are similar in structured, either appending both datasets or finding common matches. This is not my case as one dataset is a JSON of my data, and the other is a sort of metadata mapping dataset. My two input datasets are different in type.

I have also tried “Execution data” node however this node is more convenient for very isolated variables. In my mapping JSON, I have around 40 objects that map IDs with texts so it’s not very useful to use the Execution data and call it.

Please share your workflow

Share the output returned by the last node

The output of the last function will basically be two outputs: the function applied on all the input items.
I don’t really want that - I want a single output but the function should indeed use the two inputs, just in different ways because they are two different types of input.

Is it possible to handle different types of inputs in a Function node?

Many thanks for the help!

Information on your n8n setup

  • n8n version: 1.82.3
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • Operating system: MacOS Sequoia 15.3.1

Hi and welcome to the community!

Could you provide examples of these two types of data inputs and examples of what the processed output would be?

Hi Olek,

Thanks for replying.

My first data input is a very standard table, it looks something like this:

[
{
“recipient_id”: “1036610955”,
“first_name”: “Leonardo Dicaprio”,
“last_name”: “Company Fake 1”,
“question_62954838”: “523433498”
},
{
“recipient_id”: “1036610955”,
“first_name”: “Brad Pitt”,
“last_name”: “Company Fake 2”,
“question_62954835”: “523433499”
},
{
“recipient_id”: “1036610955”,
“first_name”: “Denzel Washington”,
“last_name”: “Company Fake 3”,
“question_62954835”: “523433500”
}
]

Its particularity is that some of its fields are named “question_62977157” (IDs of questions) and its values are IDs (IDs of answers), which isn’t very easy to understand/compute.

My second data input is a mapping of the question IDs and answer IDs. It looks like:
[
{
“Question ID”: 62954838,
“Question name”: “What is your favourite movie?”,
“Answer”: “Se7en”,
“Answer ID”: 523433498
},
{
“Question ID”: 62954835,
“Question name”: “Who is your favourite director to work with?”,
“Answer”: “Quentin Tarantino”,
“Answer ID”: 523433499
},
{
“Question ID”: 62954835,
“Question name”: “Who is your favourite director to work with?”,
“Answer”: “Martin Scorcese”,
“Answer ID”: 523433500
}
]

So with a simple function, my idea is to transform my first input in this format:
{
“recipient_id”: “1036610955”,
“first_name”: “Denzel Washington”,
“last_name”: “Company Fake 3”,
“Who is your favourite director to work with?”: “Martin Scorcese”
}

I wrote the function and it should work fine - the problem here is that my function node needs to take TWO different inputs, of different types: one data table, and one mapping “metadata” table.

I’ve tried the “merge” node but the point of this node is to merge datasets of similar structure, which is not my case.
I’ve tried the “execution command” node but this is for a small amount of metadata: a few fields is fine but my mapping dataset probably has 30 or 40 different answer and question IDs.

Do you know how to input 2 different nodes/datasets of different types within the same Function node? Is this possible?

If not, is there any way around this?

Hope this is clearer !
Thanks.

Very… inconvenient data structure to work with.

This should do the job.

I didn’t get rid of fields accumulated along the transformation way, but let me know if you definitely want to have only 4 fields in every output entry.

Well, the situation where a question becomes an object’s key is something that will be as difficult to work with further down the pipeline as with the fields like question_387463476.

Please consider a neater and more consistent data structure like

{
  ...
  questions: [
    {
      question: "Who is your favorite film producer?",
      answer: "Tim Burton"
    }
  ]
}

This way you may collect all Q/As related to a single recipient.

If the example above resolves your issue, please mark this post as a :white_check_mark: Solution.

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