Can't Split Out Items

Hi - we are trying to split out items with this result but it’s not working. Any thoughts?

Did you try the “Item Lists” node?

You can find the documentation here.

Yes we did. But it couldn’t find the field called “contact”. There must be something we are missing…

Hi @The_Leading_Practice , contact itself wouldn’t be a field that could be split out from the looks of it (since it’s not a list/an array). Do you just want to see all the fields inside contact at the top level? If so, you could run something like return item.contact; in a Function Item node.

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{json: {contact: {\"foo\": \"bar\", \"baz\": 2, \"qux\": [\"123\", \"abc\"]}}}]\n"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return item.contact;"
      },
      "name": "Move up contact fields",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Move up contact fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I have this saved code snippet with a brief description to use in situations like this. You need to put it in a function node and change “results” to “contact”.

Sometimes (e.g. with external APIs returning data in a different format), you will encounter nested items. This usually means the data we are interested in is its own object nested within the JSON response from the API.

Where “results” is the object/array of interest, we would modify our function above as so:

return items[0].json.results.map(item => ( { json : item } ));

Thank you for the help @automatron

Here’s my code:

return items[0].json.contact.map(item => ( { json : item } ));

Here’s the error I’m now receiving:
ERROR: Cannot read property ‘map’ of undefined

I tried these… they didn’t work.

return item.contact;
return item.json.contact;
return items[0].json.contact;

Could you share an example workflow returning the problematic data structure?

Here’s the raw output of HTTP Request node.

Items: 1

[
{
"contact": {
"id": "UVsQqOnTIdnKNnhKscIO",
"firstName": "xxxx",
"lastNameLowerCase": "xxxx",
"country": "US",
"fullNameLowerCase": "xxxx xxxx",
"locationId": "mTx5jaBRMAEHPlMfQbiX",
"tags": [
"ct appt",
"existing patient",
"zap"
],
"lastName": "xxxx",
"type": "lead",
"emailLowerCase": "xxxxx",
"dateAdded": "2021-10-29T15:03:39.000Z",
"phone": "+xxxxx",
"email": "xxxx",
"firstNameLowerCase": "xxxx",
"source": "api v1",
"customField": [
]
}
}
]

Could you share the workflow where the Function Item example I have provided isn’t working? Because when trying this on my end it is working fine using your example data (though I am still not 100% sure this is what you would like to see in the end):

Here’s my example workflow using your data:

Workflow
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const example_data = [\n{\n\"contact\": {\n\"id\": \"UVsQqOnTIdnKNnhKscIO\",\n\"firstName\": \"xxxx\",\n\"lastNameLowerCase\": \"xxxx\",\n\"country\": \"US\",\n\"fullNameLowerCase\": \"xxxx xxxx\",\n\"locationId\": \"mTx5jaBRMAEHPlMfQbiX\",\n\"tags\": [\n\"ct appt\",\n\"existing patient\",\n\"zap\"\n],\n\"lastName\": \"xxxx\",\n\"type\": \"lead\",\n\"emailLowerCase\": \"xxxxx\",\n\"dateAdded\": \"2021-10-29T15:03:39.000Z\",\n\"phone\": \"+xxxxx\",\n\"email\": \"xxxx\",\n\"firstNameLowerCase\": \"xxxx\",\n\"source\": \"api v1\",\n\"customField\": [\n]\n}\n}\n];\n\nreturn example_data.map(item => {return {json:item}});\n"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return item.contact;"
      },
      "name": "Move up contact fields",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Move up contact fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

So it would be tremendously helpful if you could share your workflow’s JSON representation to see the problem first hand.

So sorry guys! After further examination, I was using the Function node instead of Function Item.

Your solution works great! Thank you for your help!

2 Likes

Awesome, I am glad to hear that, thanks so much for confirming! Have a nice weekend and go reach out if anything else comes up :slight_smile: