How to extract key from set command?

Hey the support is fantastic, thanks for all your help so far.

I was wondering how you might extract a key from a set command? I just noticed there is no text manipulation.

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "Text1",
              "value": ".morecodebefore({someKey: 'w98dhwdoNMWnnw0n', someConfig: CONFIG })"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        540,
        280
      ]
    }
  ],
  "connections": {}
}

Hi @jtas21, to manipulate text you would currently need to use pure JavaScript I am afraid. There are a few feature requests around this though (for example A text replace node), so you might want to leave your vote there.

Community members @lublak and @pemontto have actually built some nodes for this already, but the PRs (add TextManipulation node by lublak · Pull Request #1426 · n8n-io/n8n · GitHub, WIP:✨ New transform node to manipulate data by pemontto · Pull Request #2548 · n8n-io/n8n · GitHub) are still open meaning these nodes aren’t available in the general n8n releases yet.

As for your specific example I take it you’d like to extract w98dhwdoNMWnnw0n and write it in its own field? If so, I would use JavaScript’s .match() for that and a suitable regular expression. These can be used directly inside n8n expressions. Here’s an example where I am using the Set node for this:

image

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "Text1",
              "value": ".morecodebefore({someKey: 'w98dhwdoNMWnnw0n', someConfig: CONFIG })"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        440,
        440
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "Key",
              "value": "={{$json[\"Text1\"].match(/(?:^.+someKey:\\s')(\\w+)(?:'.+$)/m)[1]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Apply Regex",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        660,
        440
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Apply Regex",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thanks for the help @MutedJam!

Will also upvote!

1 Like