Hey!
How can I convert a string object to an array?
And how to replace the elements of a string with a regular expression, and there will be several expressions. Additionally, the workflow has a list “Operator”: “Value”
Ok, you can change the string to an array using the example below:
Not sure what you want to do with the property description. Do you want to replace everything within {} with something? or do you want to extract what is inside the {}?
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
250,
300
]
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n cores: 'a,b,c,d'\n }\n },\n {\n json: {\n cores: 'a,b,c,d'\n }\n },\n]"
},
"name": "Function1",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
510,
300
]
},
{
"parameters": {
"functionCode": "const results = []\n\nfor (const item of items) {\n item.json.cores = item.json.cores.split(',')\n results.push(item);\n}\n\nreturn results;"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
740,
300
]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Function1",
"type": "main",
"index": 0
}
]
]
},
"Function1": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
}
}
}
Ricardo, thanks for the help with the array!
Yes, I want to replace all {} elements with their corresponding values from another node.
Ok, without knowing all the details I will do something as below:
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
250,
300
]
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n data: {\n \n firstname: 'ricardo',\n lastname: 'espinoza'\n }\n }\n }\n]"
},
"name": "Function1",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
510,
300
]
},
{
"parameters": {
"functionCode": "const results = []\n\nfor (const item of items) {\n let description = item.json.description\n for (const key of Object.keys(item.json.data)) {\n description = description.replace(`{${key}}`, item.json.data[key])\n }\n item.json.description = description\n results.push(item);\n}\n\nreturn results;"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1060,
410
]
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n description: \"{firstname} asasasasasas {lastname}\"\n }\n }\n]"
},
"name": "Function2",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
510,
540
]
},
{
"parameters": {
"mode": "mergeByIndex"
},
"name": "Merge",
"type": "n8n-nodes-base.merge",
"typeVersion": 1,
"position": [
790,
410
]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Function1",
"type": "main",
"index": 0
}
]
]
},
"Function1": {
"main": [
[
{
"node": "Merge",
"type": "main",
"index": 0
}
]
]
},
"Function2": {
"main": [
[
{
"node": "Merge",
"type": "main",
"index": 1
}
]
]
},
"Merge": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
}
}
}
Thanks for the replacement example, this is what I need!
But, with an array, apparently, I did not formulate the task correctly.
The result is a different design. In general, I need a list of “cores” that I can map to other lists of “cores2” through the Merge node.
Perhaps, it was necessary to write not an “array”, but a “set of objects”.
Ricardo, hello!
This script helped me a lot, but it has a relative vulnerability.
If one string contains two or more identical operators, then only the first one is replaced.
Is it possible to somehow modify this script so that all operators are replaced?
Thanks in advance).
Check the example below:
{
"nodes": [
{
"parameters": {},
"name": "Start",
"type": "n8n-nodes-base.start",
"typeVersion": 1,
"position": [
250,
300
]
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n data: {\n \n firstname: 'ricardo',\n lastname: 'espinoza'\n }\n }\n }\n]"
},
"name": "Function1",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
550,
220
]
},
{
"parameters": {
"functionCode": "const results = []\n\nfor (const item of items) {\n let description = item.json.description\n for (const key of Object.keys(item.json.data)) {\n const regex = new RegExp(`{${key}}`, \"g\");\n //description = description.replace(`{${key}}`, item.json.data[key])\n description = description.replace(regex, item.json.data[key])\n\n }\n item.json.description = description\n results.push(item);\n}\n\nreturn results;"
},
"name": "Function",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
1100,
330
]
},
{
"parameters": {
"functionCode": "return [\n {\n json: {\n description: \"{firstname} {firstname} asasasasasas {lastname}\"\n }\n }\n]"
},
"name": "Function2",
"type": "n8n-nodes-base.function",
"typeVersion": 1,
"position": [
550,
460
]
},
{
"parameters": {
"mode": "mergeByIndex"
},
"name": "Merge",
"type": "n8n-nodes-base.merge",
"typeVersion": 1,
"position": [
830,
330
]
}
],
"connections": {
"Start": {
"main": [
[
{
"node": "Function2",
"type": "main",
"index": 0
},
{
"node": "Function1",
"type": "main",
"index": 0
}
]
]
},
"Function1": {
"main": [
[
{
"node": "Merge",
"type": "main",
"index": 0
}
]
]
},
"Function2": {
"main": [
[
{
"node": "Merge",
"type": "main",
"index": 1
}
]
]
},
"Merge": {
"main": [
[
{
"node": "Function",
"type": "main",
"index": 0
}
]
]
}
}
}
Ricardo, thanks a lot!