Can we rename all occurrences of a key in an array of objects using Rename Keys node?

I have an array of objects in my workflow like so:

line_items: [
{
“id”: “00001”
“description”: “Desktop/network”,
“quantity”: “1.0000”
},
{
“id”: “00002”
“description”: “Laptop/network”,
“quantity”: “2.0000”
}
]

Can I rename the key “description” to “comment” for all elements in the above array using Rename Keys node? I am unable to narrow down the expression to replace all occurrences of a key in an array of objects.

Hi @Gowthaman_Prabhu, welcome to the community :tada:

So your data would look like this in n8n?

In this case it’s unfortunately not possible to use the Rename Keys node directly. The reason for this would be that it applies its logic to each n8n item, but in this case your line_items aren’t n8n items on their own.

What you could however do (if you’re only interested in the line items) is to split out your line_items and convert them into individual n8n items. This could be done via the Item Lists node. Afterwards you can then use the Rename Keys node to change description to comment. In a workflow, this would look like so:

This is the result of the above workflow:

If you’d like to keep your original data structure, you might need to use tiny bit of code, for example like so in the Function Item node:

Result:

Hello @MutedJam,

Thank you so much for taking time to respond. I did just that and it works as it should.

My best,
Gowthaman.J.K.P

1 Like

Glad to hear, thanks so much for confirming!