I want to split a string based on '/n' in a custom code block

Describe the problem/error/question

I have a string called pages. I want to use a custom javascript or python code block to break the string up. But the required libraries do not exist.

What is the error message (if any)?

Attribute Error .split(){
“meta”: {
“instanceId”: “3ae984276f26fdccef57a1b9bc1694b82a13e4f6272e3c1d101b9cb4444cc809”
},
“nodes”: [
{
“parameters”: {
“url”: “https://www.trialwebsite.com”,
“options”: {}
},
“id”: “018b2ea2-4cf2-4314-a48c-1eb76a3292cd”,
“name”: “HTTP Request”,
“type”: “n8n-nodes-base.httpRequest”,
“typeVersion”: 4.1,
“position”: [
780,
300
]
},
{
“parameters”: {
“globalOptions”: {
“timeToWait”: 400
},
“nodeOptions”: {},
“url”: “https://trialwebsite.com”,
“output”: {
“getPageContent”: [
{}
]
}
},
“id”: “393c35ea-ccc2-43c9-8841-9029639c9892”,
“name”: “Puppeteer”,
“type”: “n8n-nodes-puppeteer-extended.puppeteer”,
“typeVersion”: 1,
“position”: [
1000,
300
],
“credentials”: {
“n8nApi”: {
“id”: “YNWkgDPVi0c4i4Zo”,
“name”: “n8n account”
}
}
},
{
“parameters”: {
“operation”: “extractHtmlContent”,
“dataPropertyName”: “pageContent”,
“extractionValues”: {
“values”: [
{
“key”: “pages”,
“cssSelector”: “.pagination”
}
]
},
“options”: {
“trimValues”: true
}
},
“id”: “dc58f1e3-7eee-43c2-a171-8ce7ff2347f2”,
“name”: “HTML”,
“type”: “n8n-nodes-base.html”,
“typeVersion”: 1,
“position”: [
1220,
300
]
},
{
“parameters”: {
“language”: “python”,
“pythonCode”: “# Loop over input items and add a new field called ‘myNewField’ to the JSON of each on\nreturn _input.all().split(‘/n’)”
},
“id”: “41af2a65-392c-4087-9595-add7f0583ef0”,
“name”: “Code”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
1440,
300
]
},
{
“parameters”: {
“batchSize”: 1,
“options”: {
“reset”: true
}
},
“id”: “ffadb9bc-b3aa-40d0-bec9-d5c36535a0ae”,
“name”: “Split In Batches”,
“type”: “n8n-nodes-base.splitInBatches”,
“typeVersion”: 2,
“position”: [
1640,
300
]
}
],
“connections”: {
“HTTP Request”: {
“main”: [
[
{
“node”: “Puppeteer”,
“type”: “main”,
“index”: 0
}
]
]
},
“Puppeteer”: {
“main”: [
[
{
“node”: “HTML”,
“type”: “main”,
“index”: 0
}
]
]
},
“HTML”: {
“main”: [
[
{
“node”: “Code”,
“type”: “main”,
“index”: 0
},
{
“node”: “Split In Batches”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Ask Chatgpt to give you the code for an N8N code block. It will output, make take a couple tried but you get there in the end.

I used the below.

let input = items[0]?.json?.data; // replace with the actual path to your input
let result = {};

if (input) {
    input = input.replace(/\\n/g, '\n'); // Replacing string representations of newlines with actual newline characters
    
    try {
        result = JSON.parse(input); // Parsing the input string to JSON
    } catch (e) {
        console.error("Error parsing JSON:", e); // Log error if parsing fails
    }
}

return [
    {
        json: result
    }
];

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.