Split Items - ID extract

Hi,
I am newbie and haven’t found an helpful answer yet in the forum.

From my start trigger I receive this JSON and I want to split out the items and execute the workflow for each ID contained in the body, but I am just not getting how to split that. The split node just returns the same as the input.


[
{
"headers": 
{
"host": 
"xxx",
"x-real-ip": 
"xxxx",
"x-forwarded-for": 
"xxx",
"x-forwarded-proto": 
"https",
"x-forwarded-port": 
"443",
"connection": 
"close",
"content-length": 
"228",
"accept": 
"application/json, text/plain, */*",
"content-type": 
"application/x-www-form-urlencoded",
"authorization": 
"xxxx",
"user-agent": 
"axios/1.7.2",
"accept-encoding": 
"gzip, compress, deflate, br"
},
"params": 
{
},
"query": 
{
},
"body": 
{
"[ { "ID": "["56fcda38-d904-43b6-9a80-6026902b77c7","f15b5c33-cf13-4c66-9df8-8b9d81f6d0ea","5d07e183-e63e-4dd7-9dfc-6a98cffef0ac","68a79d37-3fce-4843-b226-0a9463f21ac1","35e1c9f9-7366-4b53-8446-8f465c941aee"]" } ]": 
""
},
"webhookUrl": 
"https://xxx/webhook-test/xxx",
"executionMode": 
"test"
}
]

Maybe it is an issue how the “body” is formatted?

{"[\n    {\n        \"ID\": \"[\"56fcda38-d904-43b6-9a80-6026902b77c7\",\"f15b5c33-cf13-4c66-9df8-8b9d81f6d0ea\",\"5d07e183-e63e-4dd7-9dfc-6a98cffef0ac\",\"68a79d37-3fce-4843-b226-0a9463f21ac1\",\"35e1c9f9-7366-4b53-8446-8f465c941aee\"]\"\n    }\n]":""}

It looks like your topic is missing some important information. Could you provide the following if applicable.

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

I am using the on premise version: 1.41.0

Hi @marcodworschak

Thanks for posting here and welcome to the community! :partying_face:

Yes the body output looks indeed a bit strange. You would need the format to be like this in order for the Split Out node to work properly:

[
    {
        "ID": [
            "56fcda38-d904-43b6-9a80-6026902b77c7",
            "f15b5c33-cf13-4c66-9df8-8b9d81f6d0ea",
            "5d07e183-e63e-4dd7-9dfc-6a98cffef0ac",
            "68a79d37-3fce-4843-b226-0a9463f21ac1",
            "35e1c9f9-7366-4b53-8446-8f465c941aee"
        ]
    }
]

Something like this:

I was expecting that, can I achieve that somehow with n8n ?

yeah although it’s a bit ugly but you could use the code node to transform the body content into a string first and then use some regex to replace these escape characters and extra quotes

Thank you,
solved it with.

// Get the input data
const inputData = $input.all();

// Extract the body content
const bodyContent = inputData[0].json.body;

// Extract the key (which is a JSON string) and clean it up
const key = Object.keys(bodyContent)[0];

// Remove any extra characters and parse it properly
const cleanedKey = key.replace(/\\n/g, '').replace(/\\/g, '').replace(/\"\[/g, '[').replace(/\]\"/g, ']');

// Parse the cleaned JSON string
const parsedBody = JSON.parse(cleanedKey);

// Format the output
const output = parsedBody.map(item => ({
    ID: item.ID
}));

// Return the formatted data
return output;

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