Iteration in n8n vs Integromat

Hi guys
I am fairly new to n8n - because i wanted to host my integromat flow on my own server.
However, i am struggling to find out how to iterate properly.

This is how the flow looks in integromat, as you can see i use the Iterator a lot :slight_smile:

This is the HTTP GET output, that i Iterate over:

{
        "recordLocator": "AE7KCV",
        "groupBooking": false,
        "pointOfSale": {
            "userId": "93742",
            "umUserId": 6392,
            "userName": "Web Sales",
            "userLogin": "Ecommerce",
            "userEmail": "[email protected]",
            "travelAgentName": "Ecommerce",
            "location": "CPH"
        },
        "createdDateTime": "2020-12-07T08:26:35Z",
        "modifiedDateTime": "2020-12-07T08:29:31",
        "modifiedAt": "2020-12-07T08:29:31Z",
        "comments": "",
        "reservationControls": [
            {
                "reservationNumber": "XXXXX1",
                "airline": "XX",
                "cityCode": "CPH"
            }
        ],
        "segments": [
            {
                "rph": 1,
                "statusCode": "HK",
                "count": 2,
                "departureAirportCode": "CPH",
                "arrivalAirportCode": "AAL",
                "departureDate": "2021-07-15",
                "departureTime": "09:30:00",
                "arrivalDate": "2021-07-15",
                "arrivalTime": "10:30:00",
                "rbd": "T",
                "marketingRbd": "T",
                "flightDesignator": "6I0XXX",
                "marketingFlightDesignator": "6I0XXX",
                "waitlistRank": 0,
                "fareBasisCode": "TPLURT",
                "ticketing": [
                    {
                        "passengerRph": 1,
                        "ticketNumber": "2482100356814",
                        "coupon": 1
                    },
                    {
                        "passengerRph": 2,
                        "ticketNumber": "2482100356815",
                        "coupon": 1
                    }
                ],
                "tenant": "airalsie",
                "links": []
            },
            {
                "rph": 2,
                "statusCode": "HK",
                "count": 2,
                "departureAirportCode": "CPH",
                "arrivalAirportCode": "AAL",
                "departureDate": "2021-07-22",
                "departureTime": "11:00:00",
                "arrivalDate": "2021-07-22",
                "arrivalTime": "12:00:00",
                "rbd": "T",
                "marketingRbd": "T",
                "flightDesignator": "6I0XXX",
                "marketingFlightDesignator": "6I0XXX",
                "waitlistRank": 0,
                "fareBasisCode": "TPLURT",
                "ticketing": [
                    {
                        "passengerRph": 1,
                        "ticketNumber": "2482100356814",
                        "coupon": 2
                    },
                    {
                        "passengerRph": 2,
                        "ticketNumber": "2482100356815",
                        "coupon": 2
                    }
                ],
                "tenant": "airalsie",
                "links": []
            }
        ],
        "passengers": [
            {
                "rph": 1,
                "title": "Mr.",
                "firstName": "Morten",
                "lastName": "Test",
                "sex": "M",
                "passengerTypeGroup": "ADT",
                "passengerTypeCode": "1",
                "mobile": {
                    "number": "29904567",
                    "country": "45"
                },
                "phone": {},
                "address": {},
                "documents": [
                    {
                        "birthday": "1966-08-26"
                    }
                ],
                "links": []
            },
            {
                "rph": 2,
                "title": "Mrs.",
                "firstName": "Mortine",
                "lastName": "Test",
                "sex": "F",
                "passengerTypeGroup": "ADT",
                "passengerTypeCode": "1",
                "mobile": {
                    "number": "12345678",
                    "country": "45"
                },
                "phone": {},
                "address": {},
                "documents": [
                    {
                        "birthday": "1990-10-13"
                    }
                ],
                "links": []
            }
        ],
        "infants": [],
        "ssrs": [],
        "remarks": [
            {
                "segmentRph": 1,
                "passengerRph": 1,
                "code": "BAGS",
                "additionalInfo": "HK1 BAG PC ALLOW 1",
                "status": "CONFIRMED"
            },
            {
                "segmentRph": 1,
                "passengerRph": 2,
                "code": "BAGS",
                "additionalInfo": "HK1 BAG PC ALLOW 1",
                "status": "CONFIRMED"
            },
            {
                "segmentRph": 2,
                "passengerRph": 1,
                "code": "BAGS",
                "additionalInfo": "HK1 BAG PC ALLOW 1",
                "status": "CONFIRMED"
            },
            {
                "segmentRph": 2,
                "passengerRph": 2,
                "code": "BAGS",
                "additionalInfo": "HK1 BAG PC ALLOW 1",
                "status": "CONFIRMED"
            }
        ],
        "osi": [],
        "seats": [],
        "contacts": [
            {
                "person": {
                    "lastName": "Test",
                    "firstName": "Morten",
                    "title": "Mr."
                },
                "addressContact": {
                    "address": "Street 123",
                    "postalCode": "6200",
                    "city": "City",
                    "countryCode": "DK"
                },
                "billingAddressContact": {},
                "email": "[email protected]",
                "phoneContact": {},
                "mobileContact": {
                    "number": "12345678",
                    "country": "45"
                },
                "preferredLanguage": "da"
            }
        ],
        "delegates": [],
        "currencyCode": "DKK",
        "childBookings": [],
        "links": []
    }

Goal is to get all the data[], then the data in the segments[], and then the data in the ticketing[].
As i need: ticketNumber, departureDate, flightDesignator and recordLocator to be put into Google Sheets.

I sincerely hope, that someone can help me get started here :slight_smile:

Best regards
Bill

Welcome to the community @Lountar!

If I understand it correctly would you have to create a Function-Node with the following code:

return items.map(item => item.json.segments.map(segment => { 
  return {json: segment};
})).flat();

That will split the data into different items. We have planned to soon release a Data-Transformation node which would also allow to do that without code.

Hi Jan

Thanks a lot. And thanks for taking your time to look at this.
I am getting this error:

TypeError: Cannot read property 'map' of undefined
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:1:142
    at Object.base.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/contextify.js:228:34)
    at Array.map (<anonymous>)
    at Object.base.apply (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/contextify.js:621:32)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:1:111
    at Object.<anonymous> (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes:4:2)
    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/vm2/lib/main.js:1167:29)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Function.node.js:81:31)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/src/Workflow.js:492:37)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/src/WorkflowExecute.js:424:62

Not entirely sure of what to do now. Looking forward to your data-transformation node. Let me know if you need beta- testers :wink:

Best

If you get this error it means that the data is actually different to the example output you did post above. Can you please post a screenshot of the open HTTP Request node after it did execute with “JSON” selected. Thanks!

Hi Jan

Well, the first one does not contain any data in “segments”. Maybe that’s why?


Maybe 30% of the records doesn’t contain data in “segments”.

Best

Hey @Lountar!

The incoming data does not seem to be following n8n’s data structure. Are you making a request to get only one item?

In your HTTP Request node, click on Add Option and select ‘Split into Items’. This will give you the output in the correct structure. Once we have that, can you try executing the Function node again and see if still gives you an error?

If you get the same, then your assumption must be correct. In that case, you solve this in different ways.

  1. Use IF node: You can use an IF node and check for the length for segments. (you can use the .length method!)
  2. Function node: In your existing Function node, you can add a check if segment is empty or not. And based on that, you can perform further operations.

Hope this helps :slight_smile: