Need to parse JSON and split fields

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

Hi @ebdavison, in n8n you can access JMESPath to perform small searches on the go, allowing you to for example find an object inside your field_data field where name equals FULL_NAME.

So you could do something like this here:

This workflow would leave you with these fields using your example data:

"full_name": "john smith",
"email": "[email protected]",
"first_name": "john",
"last_name": "smith"

Hope this helps!

That is brilliant! I had looked at JMESpath but could not, for the life of me, make it work or even make sense of how to call it properly. And since debugging of code nodes in the browser does not work, I could only wait for calls to my API/webhook to test it out. Made for slow work.

Thank you again, very much, for the assist!!!

1 Like

You’re most welcome, glad to hear this works for you :slight_smile:

So confused at this point. I have these two set nodes into my actual pipeline and still having an issue.

The first one seems to parse properly and find the fields (though for some reason they can from the webhook in lower case so I fixed that). The preview in the node execution shows the field properly but no output from the node is sent and so the following node has nothing to parse out.

You can see the green arrow leading in but gray arrows out …

image

Here is the field data node, opened (sorry, had to obscure the name and email but you can tell by the blurry bit that it is there):

Any thoughts on what might be causing this to NOT send any output?

I forgot to include the most important part, the errors on the split section:

image

One thing I noticed is that both items refer to item-index: "0" which I presume means “no input so I cannot find anything to work with”.

Hi @ebdavison, without knowing your workflow it’ll be hard to tell what’s going on here. I suspect you’re still using loops? If so, you might need to execute the whole workflow rather than just a single node for n8n to properly execute a node and resolving the expression.

@mutedjam Here is the whole workflow minus the actual tokens for my accounts

Hi @ebdavison, it looks like this workflow relies heavily on third party data APIs require authentication, so I won’t be able to execute it.

Perhaps you can narrow the problem down a little bit? Get rid of everything but the Parse field_data node which seems to throw the problem for you. Then add a single Set or Code before your Parse field_data node passing on the exact dataset causing the problem for you.

Thank you!

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