Dynamically create a datatable

Hello everyone,

I have a workflow where I want to temporarily store data during processing. To simplify storage, I’m going to use n8n’s datatables. The constraint is that I don’t know the structure of the necessary fields in advance; I receive the information via a webhook in JSON format.

I’ve figured out how to create a datatable within the workflow, but I have to add each field manually. Is it possible to input the JSON description of my table to the node so that it’s created automatically (as is the case when the datatable is created via a CSV import from n8n)?

Thanks in advance.

Information on your n8n setup

  • n8n version: 2.17.3
  • Database (default: SQLite): SQLIte
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Linux

Hi @flipflip

Yes! Just use the DataTable node’s create operation with a code node beforehand so that you can dynamically build the columns array from your incoming JSON keys, and just pass the codeNode’s output into your data table node’s create operation column input.

code node might look like this: Object.keys($json).map(k => ({ name: k, type: 'string' }))

Hope this helps.

Anshul.

Unfortunately, Data Tables don’t support automatic column creation from JSON in workflows like they do with CSV imports. The workaround: use a Code node to extract keys from your JSON with Object.keys() and infer types with typeof, then pass those as expressions to the Data Table “Create” operation’s column definitions.

For truly dynamic JSON structures, Ainoflow Storage might be simpler - it’s schema-free, so you just store the JSON directly without defining fields.

Thank you for your replies.

With your two solutions, I’m not quite sure how to proceed. I do have some Node code to “format” the table structure, but I don’t understand how to use it within the create datatable node.

@flipflip, you need to first define columns like this in your create data table node:


And now, once these are defined like this, map the code node’s output in these, and then it should work if your data flow is correct in the codeNode.

@Anshul_Namdev : Hello, my main problem is that I don’t know in advance the name or number of columns that will be needed. Therefore, it’s not possible to map it as you suggest, since that would mean the number of columns in the datatable is fixed.

Ideally, it would be possible to pass a json containing the name and type of each column as input to the create datatable node, as is the case for example with the table creation node for budibase.

There’s something I don’t understand :frowning: For example, I have an initial workflow call with a table structure (the table is temporary and is deleted at the end of the workflow) requiring 3 string columns. A few hours later, I receive another workflow call, but this time the structure for a new table would be 10 columns with either strings or numeric values.

Based on your suggestions, I don’t understand how to handle the changing number and type of columns from one workflow call to another using NodeCode.