Serializing items and removing some strings off a value

Hello!

I’m using the following code to get item wise list of queries.

const serialItems = [];

for (const element of items[0].json) {
  serialItems.push({json: element});
}

return serialItems;

Is there a way to find and replace some strings in the values of a specific key?

Here’s my small workflow:

mock data and workflow
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const serialItems = [];\n\nfor (const element of items[0].json) {\n  serialItems.push({json: element});\n}\n\nreturn serialItems;"
      },
      "name": "serializing1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -300,
        -180
      ]
    },
    {
      "parameters": {
        "functionCode": "return[{json:\n    [\n      {\n        \"COUNT\": \"+1-1\"\n      },\n      {  \n        \"COUNT\": \"+2-1\"\n      },\n      {\n        \"COUNT\": \"+3-1\"\n      }\n    ]\n  }\n]"
      },
      "name": "mock json1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -500,
        -180
      ]
    }
  ],
  "connections": {
    "mock json1": {
      "main": [
        [
          {
            "node": "serializing1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Is it possible to remove “+” and “-” in “COUNT” key for all the items at the time of running the 2nd node itself?

image

Any pointers are highly appreciated.

Use the code below in the function node.

const serialItems = [];

for (const element of items[0].json) {
  serialItems.push({json: { COUNT: element.COUNT.replace('+', '').replace('-', '') }});
}

return serialItems;
1 Like

Thank you @RicardoE105!
However, this code removes all keys but “COUNT” from the query. How can I prevent it?

I do not get it. Can you share the output as you want it exactly?

I just realized that I missed out the “key” from my mock data. This is what the output should be:

[
{
"COUNT": "11",
"key":"value"
},
{
"COUNT": "21",
"key":"value2"
},
{
"COUNT": "31",
"key":"value3"
}
]

This is what I get:

[
{
"COUNT": "11"
},
{
"COUNT": "21"
},
{
"COUNT": "31"
}
]
const serialItems = [];

for (const element of items[0].json) {
  serialItems.push({json: { ...element, COUNT: element.COUNT.replace('+', '').replace('-', '') }});
}

return serialItems;
1 Like

Thank you! This works.