Extracting nested string JSON

Describe the problem/error/question

so i have been trying all day long to just get the JSON string parsed to JSON which is nested in the JSON returned by Openai, but even with ChatGPT I could not solve it…

What is the error message (if any)?

Error: Code doesn't return items properly
    at JavaScriptSandbox.validateRunCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Sandbox.js:51:19)
    at JavaScriptSandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/JavaScriptSandbox.js:51:21)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:116:25)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:658:19)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:631:53

Please share your workflow

{
“meta”: {
“instanceId”: “7178564f8c4d877086ab82440ea3cc7d4c8fb9f906269d9f8b1616a2d2331997”
},
“nodes”: [
{
“parameters”: {
“pollTimes”: {
“item”: [
{
“mode”: “everyHour”
}
]
},
“databaseId”: {
“__rl”: true,
“value”: “7ef755da-3fe8-4d5c-bf86-219b94412856”,
“mode”: “list”,
“cachedResultName”: “Contents”,
“cachedResultUrl”: “https://www.notion.so/7ef755da3fe84d5cbf86219b94412856
}
},
“id”: “2b347d00-feb4-4176-a17b-aae5ce792fd3”,
“name”: “Notion Trigger (Beta)”,
“type”: “n8n-nodes-base.notionTrigger”,
“typeVersion”: 1,
“position”: [
720,
460
],
“credentials”: {
“notionApi”: {
“id”: “1”,
“name”: “Notion account”
}
}
},
{
“parameters”: {
“conditions”: {
“string”: [
{
“value1”: “={{ $json.Status }}”,
“value2”: “=Idea”
}
]
},
“combineOperation”: “any”
},
“id”: “9409e4fd-f778-4fb6-9aac-2b0350dfc765”,
“name”: “IF”,
“type”: “n8n-nodes-base.if”,
“typeVersion”: 1,
“position”: [
940,
460
]
},
{
“parameters”: {
“resource”: “image”,
“prompt”: “={{ $json.text }}, high definition, blurred background, 3D render, simple”,
“responseFormat”: “imageUrl”,
“options”: {
“n”: 1
}
},
“id”: “189f17be-769f-463f-b572-49bdc0f42ec7”,
“name”: “OpenAI2”,
“type”: “n8n-nodes-base.openAi”,
“typeVersion”: 1,
“position”: [
1720,
360
],
“credentials”: {
“openAiApi”: {
“id”: “2”,
“name”: “OpenAi account”
}
}
},
{
“parameters”: {
“person”: “[empty”,
“shareMediaCategory”: “IMAGE”,
“binaryPropertyName”: “=data {{ $json.url }}”,
“additionalFields”: {
“title”: “”
}
},
“id”: “78c8e0eb-ad90-4cfd-b947-b0a0211d787b”,
“name”: “LinkedIn”,
“type”: “n8n-nodes-base.linkedIn”,
“typeVersion”: 1,
“position”: [
2160,
340
],
“credentials”: {
“linkedInOAuth2Api”: {
“id”: “vBGzFY6wy9C5kYVK”,
“name”: “LinkedIn account”
}
}
},
{
“parameters”: {},
“id”: “2b06393a-d801-46ab-95e1-0ab6dccfd53f”,
“name”: “Merge”,
“type”: “n8n-nodes-base.merge”,
“typeVersion”: 2,
“position”: [
1920,
580
]
},
{
“parameters”: {
“resource”: “chat”,
“model”: “gpt-4”,
“prompt”: {
“messages”: [
{
“role”: “assistant”,
“content”: “=General:\nOutput the content as JSON with a property for each: headline, post-text,image-prompt\n\n\nFirst:\nAct as a senior copywriter and senior marketeer, write a linkedin post without emojis for me ( I am a creative director fro music and sound and UX/UI Sound Desiner) and with a attention grabbing headline and body text based on: {{ $json.Description }}. Create leads and drive them by CTA to contact me per dm or per mail [email protected] or send them to my website www.paul-louis.design Add fitting hashtags at the end Always post in english. Format the text for light reading.\n\nThen:\nact as a senior prompt engineer for image generating ai’s. create a prompt for a picture for this post. the picture has to be relevant and supporting to the post. it should not be dark or any other sort of unpleasant. it should be thougth provoking but welcoming and inviting. always in the 2d flat design style and minimalistic. sometimes neo-brutalism is fine to when it is fitting the topic from the post.\n\n\n\n”
}
]
},
“options”: {}
},
“id”: “705f0da4-f1ea-43f1-a380-13c15290ed7f”,
“name”: “OpenAI3”,
“type”: “n8n-nodes-base.openAi”,
“typeVersion”: 1,
“position”: [
1300,
360
],
“credentials”: {
“openAiApi”: {
“id”: “2”,
“name”: “OpenAi account”
}
}
},
{
“parameters”: {
“resource”: “databasePage”,
“operation”: “update”,
“pageId”: {
“__rl”: true,
“value”: “={{ $json["id"] }}”,
“mode”: “id”
},
“simple”: false,
“propertiesUi”: {
“propertyValues”: [
{
“key”: “Status|status”,
“statusValue”: “Ready to publish”
}
]
},
“options”: {}
},
“id”: “5c046e6a-f5f4-4b47-b631-cf0bf1da69e8”,
“name”: “Notion”,
“type”: “n8n-nodes-base.notion”,
“typeVersion”: 2,
“position”: [
2420,
480
],
“credentials”: {
“notionApi”: {
“id”: “1”,
“name”: “Notion account”
}
}
},
{
“parameters”: {
“mode”: “chooseBranch”,
“output”: “input2”
},
“id”: “7bc7469b-c9dc-4499-8f7a-e264445bf824”,
“name”: “Merge1”,
“type”: “n8n-nodes-base.merge”,
“typeVersion”: 2.1,
“position”: [
2160,
860
]
},
{
“parameters”: {
“jsCode”: “\n”
},
“id”: “89cbe5dc-a331-432f-b7aa-97f9e5e66311”,
“name”: “Code1”,
“type”: “n8n-nodes-base.code”,
“typeVersion”: 2,
“position”: [
1260,
860
]
}
],
“connections”: {
“Notion Trigger (Beta)”: {
“main”: [
[
{
“node”: “IF”,
“type”: “main”,
“index”: 0
},
{
“node”: “Merge1”,
“type”: “main”,
“index”: 1
}
]
]
},
“IF”: {
“main”: [
[
{
“node”: “OpenAI3”,
“type”: “main”,
“index”: 0
}
]
]
},
“OpenAI2”: {
“main”: [
[
{
“node”: “Merge”,
“type”: “main”,
“index”: 0
}
]
]
},
“Merge”: {
“main”: [
[
{
“node”: “LinkedIn”,
“type”: “main”,
“index”: 0
},
{
“node”: “Merge1”,
“type”: “main”,
“index”: 0
}
]
]
},
“OpenAI3”: {
“main”: [
[
{
“node”: “Merge”,
“type”: “main”,
“index”: 1
},
{
“node”: “OpenAI2”,
“type”: “main”,
“index”: 0
},
{
“node”: “Code1”,
“type”: “main”,
“index”: 0
}
]
]
},
“Merge1”: {
“main”: [
[
{
“node”: “Notion”,
“type”: “main”,
“index”: 0
}
]
]
}
}
}

(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

got the solution:

const contentString = items[0].json.message.content;
let parsedContent;

try {
parsedContent = JSON.parse(contentString);
} catch (e) {
return [{ json: { error: ‘Invalid JSON format’ } }];
}

return [
{
json: parsedContent
}
];

3 Likes

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