How to add a constant to an existing key name?

Hey!
I tried it myself, but I can’t add the constant “item-” to each key with a name as a number.
How can I do that?

Note: The number of keys with the name as a number is always different.

An example of initial data:

You need to pull the following JSON construct:

"id": 1,
"meta_value": {
"item-0": {
"name": "Main Material:",
"value": "Fabric" Greta "65% polyester / 35% cotton, density 210 g / m2, VO-impregnation"
},
"item-1": {
"name": "Insulation:",
"value": "" Sintepon "480 g / m2 back and shelves, 240 g / m2 sleeves and trousers, 120 g / m2 hood"
},
"item-2": {
"name": "Lining material:",
"value": "100% polyethylene; 60 g / m2"
},
...

The function node to do that should look similar to:

for (let index = 0; index < items.length; index++) {
  const newData = {};
  for (let i = 0; i <  Object.keys(items[index].json.meta_value).length; i++) {
    newData[`item-${i}`] = items[index].json.meta_value[Object.keys(items[index].json.meta_value)[i]]
    items[index].json['meta_value'] = newData;
  }
}

return items;
Example workflow
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      id: 1,\n      meta_value: {\n        0: {\n        \n        },\n        1: {\n        \n        }\n      }\n    }\n  }\n]\n"
      },
      "name": "Config 1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        540,
        300
      ],
      "notesInFlow": true,
      "notes": "Mockup data"
    },
    {
      "parameters": {
        "functionCode": "\nfor (let index = 0; index < items.length; index++) {\n  const newData = {};\n  for (let i = 0; i <  Object.keys(items[index].json.meta_value).length; i++) {\n    newData[`item-${i}`] = items[index].json.meta_value[Object.keys(items[index].json.meta_value)[i]]\n    console.log(newData)\n    items[index].json['meta_value'] = newData;\n  }\n}\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        740,
        300
      ]
    }
  ],
  "connections": {
    "Config 1": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @RicardoE105 !
Thanks for your help, but your code didn’t work on my data and your example data.
The code renames only the first element “0”.

Hi again, @RicardoE105!
Can you fix your code for my data?)

Change the function node to:

for (let index = 0; index < items.length; index++) {
  const newData = {};
  const length = Object.keys(items[index].json.meta_value).length;
  for (let i = 0; i < length; i++) {
    newData[`item-${i}`] = items[index].json.meta_value[Object.keys(items[index].json.meta_value)[i]]
  }
  items[index].json['meta_value'] = newData;
}

return items;
1 Like

Thanks @RicardoE105!
Everything works great!

@RicardoE105, hello again!

I started testing your script in my workflow and got the following results:

  1. With a small set of input data (up to about 160 rows), everything works well.

  2. In my case there are 3032 rows and an error appears on the function node.

I have defined a 162 rows boundary for the amount of incoming data (up to this limit, within my data, the script works well).

What is the reason for this? How can this be fixed?

My workfow:

Function node:

Error function node:

I found a bug. This is due to a different structure within the incoming data.
I’ll try to figure it out myself.
I apologize for disturbing you.

I figured it out myself.
Filtered out all non-null strings using the switch node.

Glad that you found the issue. Have fun.

1 Like