Describe the problem/error/question
I have the following JSON that comes from a previous node in my workflow:
[
{
"created_time": "2023-11-07T04:22:26+0000",
"id": "352797804084964",
"field_data": [
{
"name": "EMAIL",
"values": [
"[email protected]"
]
},
{
"name": "FULL_NAME",
"values": [
"john smith"
]
},
{
"name": "0",
"values": [
"Yes"
]
}
]
}
]
I am currently using a TextManipulation node to split the FULL_NAME field out into first_name and last_name. The problem, which I have just discovered, is that the order of the elements (FULL_NAME, EMAIL, etc.) are not guaranteed to be in the same order. My current node is assuming that FULL_NAME is [1] and EMAIL is [2] in the field_data array/list. However, as you can see in the JSON above, that order is now different. This data is returned from FB in response to a leadgen query.
Seems I need to loop through this data to find the NAMED array elements and then split it out. I think a code node would work but I could not get it to work.
I tried this to no avail:
var data = {};
data = $input.all();
for (const item of $input.all()) {
item.json.myNewField = 1;
for (const field in item.json.field_data) {
if (field.json.name == "FULL_NAME") {
data['first_name'] = field.json.values[0].split(" ")[0];
data['last_name'] = field.json.values[0].split(" ")[1];
} else if (field.name == "EMAIL") {
data['email'] = field.json.values[0];
}
}
data['id'] = item.json.id;
}
return data;
What is the error message (if any)?
no resulting output / no error
Share the output returned by the last node
[
{
"created_time": "2023-11-07T04:22:26+0000",
"id": "352797804084964",
"field_data": [
{
"name": "EMAIL",
"values": [
"[email protected]"
]
},
{
"name": "FULL_NAME",
"values": [
"joe smith"
]
},
{
"name": "0",
"values": [
"Yes"
]
}
],
"first_name": "joe",
"last_name": "smith",
"email": "[email protected]"
}
]
But what I get is:
...
"last_name": "smith",
"first_name": "joe",
"email": "Yes"
...
Information on your n8n setup
- n8n version: 1.11.1
- Database (default: SQLite): MySQL
- n8n EXECUTIONS_PROCESS setting (default: own, main): ??
- Running n8n via (Docker, npm, n8n cloud, desktop app): docker
- Operating system: Linux ubuntu