If merge doesn't work when a key:value may or may not be present, what should I use?

I need to know how I can reformat data when arbitrary fields may not be populated.

I can’t share my workflow. It’s too large, around 40,000 characters.

If the merge node worked as I expect it to, after the switch, the set nodes would set, and then the merge nodes would merge the data they received and pass it on. I have the “Always output data” switch toggled ‘ON’.

But that’s not what happens. If any of the paths fails to execute, then the whole process stops. No error.

How can I process this data efficiently? Stringing 19 'IF" nodes is a nightmare and I haven’t found a method that creates the data format I want and doesn’t have a bunch of extra data in it. Then, there’s the fact that the switch automatically removes the fields I don’t want.

The weird part is, in this loop, if the loop is not closed, n8n doesn’t seem to care there are missing fields.

But if I close the loop, if any field is missing, it stops cold.

I’m starting with data like this:

{
    "hudu": {
        "id": 6903,
        "company_id": 5,
        "asset_layout_id": 30,
        "slug": "emilyp-mba-pdx-m1-2e058df16fcc",
        "name": "EmilyP MBA PDX M1",
        "primary_serial": "FVFF70NRQ6LT",
        "primary_mail": null,
        "primary_model": "MacBook Air (M1, 2020)",
        "primary_manufacturer": "Apple, Inc",
        "company_name": "ECONorthwest",
        "object_type": "Asset",
        "asset_type": "Computers",
        "archived": false,
        "url": "https://kginger.huducloud.com/a/emilyp-mba-pdx-m1-2e058df16fcc",
        "created_at": "2024-08-03T15:11:50.585Z",
        "updated_at": "2024-11-22T16:11:13.112Z",
        "fields": [
            {
                "id": 71332,
                "value": null,
                "label": "General",
                "position": 1
            },
            {
                "id": 71333,
                "value": "picha",
                "label": "Contact Name",
                "position": 2
            },
            {
                "id": 71334,
                "value": "picha",
                "label": "Last User",
                "position": 3
            },
            {
                "id": 71335,
                "value": "11/22/2024",
                "label": "Last Report",
                "position": 4
            },
            {
                "id": 71336,
                "value": "11/22/2024",
                "label": "Last Workflow Update",
                "position": 5
            },
            {
                "id": 71337,
                "value": "2024-03-08T00:00:00.000Z",
                "label": "Purchase Date",
                "position": 6
            },
            {
                "id": 71338,
                "value": "System record updated by n8n.",
                "label": "Notes",
                "position": 7
            },
            {
                "id": 71339,
                "value": null,
                "label": "Hardware",
                "position": 8
            },
            {
                "id": 71340,
                "value": "Apple, Inc",
                "label": "Manufacturer Name",
                "position": 9
            },
            {
                "id": 71341,
                "value": "MacBook Air (M1, 2020)",
                "label": "Model",
                "position": 10
            },
            {
                "id": 71342,
                "value": "FVFF70NRQ6LT",
                "label": "Serial Number",
                "position": 11
            },
            {
                "id": 71343,
                "value": "Apple M1",
                "label": "CPU",
                "position": 12
            },
            {
                "id": 71344,
                "value": "16",
                "label": "Installed RAM",
                "position": 13
            },
            {
                "id": 71345,
                "value": "494",
                "label": "Boot Volume Capacity",
                "position": 14
            },
            {
                "id": 71346,
                "value": "29",
                "label": "Boot Volume Usage",
                "position": 15
            },
            {
                "id": 71347,
                "value": null,
                "label": "Warranty Expires At",
                "position": 16
            },
            {
                "id": 71348,
                "value": null,
                "label": "Software",
                "position": 17
            },
            {
                "id": 71349,
                "value": "mac",
                "label": "Operating System",
                "position": 18
            },
            {
                "id": 71350,
                "value": "15.1",
                "label": "OS Version",
                "position": 19
            },
            {
                "id": 71351,
                "value": "OS Notes",
                "label": "Operating System Notes",
                "position": 20
            },
            {
                "id": 71352,
                "value": null,
                "label": "Network",
                "position": 21
            },
            {
                "id": 71353,
                "value": "emilyp-mba-pdx-m1",
                "label": "Hostname",
                "position": 22
            },
            {
                "id": 71354,
                "value": "50:ed:3c:2c:30:5e",
                "label": "MAC Address",
                "position": 23
            },
            {
                "id": 71355,
                "value": "10.0.0.85",
                "label": "Primary IP",
                "position": 24
            },
            {
                "id": 71356,
                "value": null,
                "label": "Site",
                "position": 26
            },
            {
                "id": 71357,
                "value": "ECONorthwest",
                "label": "Location Name",
                "position": 27
            },
            {
                "id": 71358,
                "value": "EmilyP MBA PDX M1",
                "label": "Name",
                "position": 28
            },
            {
                "id": 71359,
                "value": null,
                "label": "Removed",
                "position": 29
            }
        ],
        "cards": []
    }
}

I want to reformat to this:

[
    {
        "assets": [
            {
                "id": 6895,
                "company_id": 5,
                "asset_layout_id": 30,
                "name": "MatthewK MBA M2",
                "primary_serial": "FPN42C3Y2V",
                "primary_model": "MacBook Air (M2, 2022)",
                "primary_manufacturer": "Apple, Inc",
                "company_name": "ECONorthwest",
                "url": "https://kginger.huducloud.com/a/matthewk-mba-m2-729e61fc625d",
                "fields": [
                    {
                        "contact_name": "kitchen",
                        "last_user": "kitchen",
                        "last_report": "11/22/2024",
                        "last_workflow_update": "11/22/2024",
                        "purchase_date": "2024-02-19T02:26:03.000Z",
                        "manufacturer_name": "Apple, Inc",
                        "model": "MacBook Air (M2, 2022)",
                        "serial_number": "FPN42C3Y2V",
                        "cpu": "Apple M2",
                        "installed_ram": "16"
                    }
                ]
            }
        ]
    }
]

How can I do so?

I was going to include my workflow. But it’s evidently too large for the forum software.

Information on your n8n setup

  • n8n version: 1.68.0
  • Database (default: SQLite): PostgresQL 15
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Default
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker on Digital Ocean
  • Operating system: Ubuntu Linux 22.04

Hey @russellkg , JMESPath to the rescue:

2 Likes

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