Split formula not working well

Hi
I have this flow (JSON - Function - SET) that It gives 1 JSON result and then I split using a function formula and “clean” using SET. I have this issue when where some values are not well split between event and bookings fields, seems some values are missing and then they overlap with wrong label. Thank you very much.

From “SET”

JSON feed

FLOW

{
  "nodes": [
    {
      "parameters": {
        "url": "http://www.trumba.com/calendars/brisbane-city-council.json",
        "options": {}
      },
      "name": "Brisbane",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        110,
        850
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "cost",
              "value": "={{$json[\"customFields\"][4][\"value\"]}}"
            },
            {
              "name": "age",
              "value": "={{$json[\"customFields\"][5][\"value\"]}}"
            },
            {
              "name": "title",
              "value": "={{$json[\"title\"]}}"
            },
            {
              "name": "location",
              "value": "={{$json[\"location\"]}}"
            },
            {
              "name": "startdatetime",
              "value": "={{$json[\"startDateTime\"]}}"
            },
            {
              "name": "enddatetime",
              "value": "={{$json[\"endDateTime\"]}}"
            },
            {
              "name": "allday",
              "value": "={{$json[\"allDay\"]}}"
            },
            {
              "name": "eventimage",
              "value": "={{$json[\"eventImage\"][\"url\"]}}"
            },
            {
              "name": "eventtype",
              "value": "={{ $node[\"Split up\"].json[\"customFields\"][\"3\"][\"value\"] }}"
            },
            {
              "name": "bookings",
              "value": "={{$json[\"customFields\"][6][\"value\"]}}"
            },
            {
              "name": "bookingrequired",
              "value": "={{$json[\"customFields\"][7][\"value\"]}}"
            }
          ],
          "number": [
            {
              "name": "EventID",
              "value": "={{$json[\"eventID\"]}}"
            }
          ]
        },
        "options": {}
      },
      "name": "Keep Brisbane  Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        430,
        850
      ]
    },
    {
      "parameters": {
        "functionCode": "const newItems = [];\nfor (const item of items[0].json) {\n  newItems.push({json: item});\n}\nreturn newItems;"
      },
      "name": "Split up",
      "type": "n8n-nodes-base.function",
      "position": [
        270,
        850
      ],
      "typeVersion": 1
    }
  ],
  "connections": {
    "Brisbane": {
      "main": [
        [
          {
            "node": "Split up",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split up": {
      "main": [
        [
          {
            "node": "Keep Brisbane  Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Yes, this is expected since you are looking into the custom_fields array, and the length varies between records.

For example:

The record with eventID: 150048287 has the property “booking required” at position 7

The record with eventID: 151289440 has the property “booking required” at position 4

The expression you are using is: {{$json["customFields"][7]["value"]}}

As you can see for the record with eventID 150048287 the position (7) exists but for the record with eventID 151289440 the position (4) does not exist.

To fix this you can map the custom fields to be an object instead of an array of objects. Something like:

{
   "customFields": {
        "Bookings required": "Yes"
   }
}

Hey @Jorge_M!

To add to what Ricardo mentioned, you can refer to the code snippet mentioned in the documentation.

Hi @harshil1712,
I am genuinely confused :smiley: , Ricardo mentions “map to object instead of array of objects”, but your link seems to point to the other way around or I am missing the nomenclatures correctly?

Hey @Jorge_M,

Currently, CustomFields contains the list of items, which is nested inside the Event data. You will have to extract the CustomFields from the Event data to get the results that you want.

Hi @harshil1712, @RicardoE105,
Not all worked for me, thanks a lot for this :slight_smile:, but was not able to perform all, I was able to split into individual items,

But still not able to pass from:

{

“label”: “Bookings”,

“value”: "No bookings required. "
},

{

“label”: “Bookings required”,

“value”: “No”

}

To:

{
“Bookings”: “No bookings required.&#160”,
“Bookings required”: “No”
}

Check the example below:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      \"label\": \"Bookings\",\n       \"value\": \"No bookings required.\"\n    }\n  },\n    {\n    json: {\n      \"label\": \"Bookings Required\",\n       \"value\": \"No\"\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        250,
        410
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = {};\n\nfor (const item of items) {\n    results[item.json.label] = item.json.value\n}\n\nreturn [\n  {\n    json: results,\n  }\n]"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        560,
        410
      ]
    }
  ],
  "connections": {
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @RicardoE105,
Thanks but I am doing still something wrong. I get empty values:

{
  "nodes": [
    {
      "parameters": {
        "url": "http://www.trumba.com/calendars/brisbane-city-council.json",
        "options": {}
      },
      "name": "Brisbane1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -690,
        550
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Split up3",
      "type": "n8n-nodes-base.function",
      "position": [
        -480,
        550
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "results",
              "value": "={{$json[\"customFields\"]}}"
            }
          ],
          "number": []
        },
        "options": {}
      },
      "name": "BDATA",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        -300,
        560
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = {};\n\nfor (const item of items) {\n    results[item.json.label] = item.json.value\n}\n\nreturn [\n  {\n    json: results,\n  }\n]"
      },
      "name": "Function8",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -100,
        560
      ]
    }
  ],
  "connections": {
    "Brisbane1": {
      "main": [
        [
          {
            "node": "Split up3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split up3": {
      "main": [
        [
          {
            "node": "BDATA",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "BDATA": {
      "main": [
        [
          {
            "node": "Function8",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

I see DATA it still seems NESTED, and I get empty results in the end. Thanks :slight_smile:.

{
"results": [
{
"fieldID": 22503,
"label": "Venue",
"value": "Museum of Brisbane, Brisbane City",
"type": 17
},
 {
"results": [
{
"fieldID": 22503,
"label": "Venue",
"value": "Museum of Brisbane, Brisbane City",
"type": 17
},

Hi @RicardoE105, @harshil1712,
Any idea how to achieve the:

{
“Bookings”: “No bookings required”,
“Bookings required”: “No”
}

The previous solution gives always empty, not sure what is going wrong.

I also tried to see if I could isolate the labels itself using:

const results = {}
const data = items[0].json

for (const key of Object.keys(data)) {
  if (data[key].includes('Bookings')) {
    results[key] = data[key]
  }
}

return [ { json: results } ];

But in return, I also get empty fields. Thank you very much.

@Jorge_M check the example below:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        290
      ]
    },
    {
      "parameters": {
        "url": "http://www.trumba.com/calendars/brisbane-city-council.json",
        "options": {}
      },
      "name": "Brisbane1",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        520,
        290
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Split up3",
      "type": "n8n-nodes-base.function",
      "position": [
        770,
        290
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nfor (const item of items) {\n    const data = {};\n    for (const customField of item.json.customFields) {\n        data[customField.label] = customField.value\n    }\n    results.push({ json: data })\n  }\n\nreturn results;"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        1020,
        290
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Brisbane1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Brisbane1": {
      "main": [
        [
          {
            "node": "Split up3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split up3": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Wow, that was quick :slight_smile:, thank you very much.

Interesting, Is there a way to see how the JSON is formed? When looking at Brisbane it works flawlessly, but in Redland (Which through over-caffeine eyes) it looks same, it is not working for Redland.

{
  "nodes": [
    {
      "parameters": {
        "url": "https://www.trumba.com/calendars/redland-city-council.json",
        "options": {}
      },
      "name": "Redland",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -620,
        1350
      ]
    },
    {
      "parameters": {
        "url": "http://www.trumba.com/calendars/brisbane-city-council.json",
        "options": {}
      },
      "name": "Brisbane2",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -617,
        1149
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Split up6",
      "type": "n8n-nodes-base.function",
      "position": [
        -370,
        1150
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nfor (const item of items) {\n    const data = {};\n    for (const customField of item.json.customFields) {\n        data[customField.label] = customField.value\n    }\n    results.push({ json: data })\n  }\n\nreturn results;"
      },
      "name": "Function11",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -117,
        1149
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.map(item => {\n  return {\n    json: item\n  }\n});\n"
      },
      "name": "Split up7",
      "type": "n8n-nodes-base.function",
      "position": [
        -370,
        1350
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "functionCode": "const results = [];\n\nfor (const item of items) {\n    const data = {};\n    for (const customField of item.json.customFields) {\n        data[customField.label] = customField.value\n    }\n    results.push({ json: data })\n  }\n\nreturn results;"
      },
      "name": "Function14",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -110,
        1350
      ]
    }
  ],
  "connections": {
    "Redland": {
      "main": [
        [
          {
            "node": "Split up7",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Brisbane2": {
      "main": [
        [
          {
            "node": "Split up6",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split up6": {
      "main": [
        [
          {
            "node": "Function11",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split up7": {
      "main": [
        [
          {
            "node": "Function14",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Just ran it and it works the same with both cases. Not sure what it’s not working for you.

1 Like

My apologies @RicardoE105,
I copied the wrong function :rofl: :sweat_smile: . Thank you very much once again :slight_smile: .

np it happens. Glad that it works now. Have fun.