How to unify JSON table header name?

Hello

I am trying to compare two columns from a sheet every day. There is an IF node to proceed to the next step. Since my sheet header is the current day, it is dynamic. How can we standardize the names, e.g., col-1, col-2, instead of ‘2024-04-22’, ‘2024-04-21’, so that we can use them as expressions for the IF node?

Thanks

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

Here is a pretty static but clean solution if you always know the number of columns you will have to deal with.

You can get an array of the keys using the .keys() method. A key is the name of a value in json.
Then you can use that key to query the json just like how you were.

Use an edit fields node and manually name the columns then use: {{ $json[ $json.keys()[1] ] }} in the value to get the value of that index of json. Increment the 1 for each new value.
I started at one instead of 0 because 0 would be the row numbers

See the node below

You can also just do this straight into the if node, but this may make it a little easier to know what’s happening.

Also, this will only work if you have no other data in your json

Let me know if you have any questions

Hello. I kind of understand the solution but I cannot get it working. Any idea what I am doing wrong here? *It says undefined

Thank you very much


You just have a few typos. I explained below but just go to the bottom and copy the last thing and that should work

You can access a key in two ways, dot and bracket notation (json.key and json['key'])
You need to use brackets of there is any spaces or if it is an expression.

$json.keys() is a method (aka a function) to get an array of the keys. Then if we add a number in brackets to the result of that, we get the item at that index in the array. $json.keys()[1] means the second key in the list

So you need to query your json by that second key in the list {{ $json[ $json.keys()[1] ] }}

So you call the .keys() inside of the query.

Hello.
I really tried many way, .keys() does not seem to work here in the expression at all

So I used ChatGPT help me to write a JS to convert the data. .keys() works here.

Now I want to replace my whole static JSON array. I have tried $input.all() does not work as expect. I have also tried $input.all()[0].json. It shows ERROR… Any idea how could we retrieve the JSON array from from the previous node? Thanks…

I think I have the issue…
It is to use $input.all().map(item => item.json);

That’s strange that it wouldn’t work in line but that should work too