XML to JSON Explicit Array only on the highest level

Hi all,

I have a XML that I need to process. Sometimes I have multiple records and sometimes I only have one. When I have multiple records all is good.

But when I only have one and try to use the item node to split into Items it goes wrong.
image
As it is not an array so it gives an error.
image

I saw I can sorta fix this by changing it to make an explicit array with the XML → JSON node.

But it transforms all values into arrays. Where I just need one level to be an array.
XML as received:

{
  "nodes": [
    {
      "parameters": {
        "options": {
          "explicitArray": false
        }
      },
      "name": "XML",
      "type": "n8n-nodes-base.xml",
      "typeVersion": 1,
      "position": [
        1180,
        280
      ],
      "executeOnce": false
    },
    {
      "parameters": {
        "fieldToSplitOut": "Beeyond.Supplier",
        "options": {}
      },
      "name": "Records Splitsen",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        1380,
        280
      ]
    }
  ],
  "connections": {
    "XML": {
      "main": [
        [
          {
            "node": "Records Splitsen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Is there any way I can configure this Node to only have a specific level made into an array?

n8n version .151

Hi @Bram_Knuever, the simplest way I can think of would be to use JavaScript’s .concat() before the Item Lists node. This method accepts both single objects and arrays but will always return an array (which is what is expected by the Item Lists node in your example).

Here is an example using the Function Item node to run .concat:

Example Workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "fieldToSplitOut": "Beeyond.Supplier",
        "options": {}
      },
      "name": "Records Splitsen",
      "type": "n8n-nodes-base.itemLists",
      "typeVersion": 1,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    Beeyond: {\n      Supplier: \"foo\"\n    }\n  }\n}, {\n  json: {\n    Beeyond: {\n      Supplier: [\"foo\", \"bar\"]\n    }\n  }\n}]"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.Beeyond.Supplier = [].concat(item.Beeyond.Supplier);\n\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        680,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Data": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "FunctionItem": {
      "main": [
        [
          {
            "node": "Records Splitsen",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @MutedJam
Thanks!
I expected that there should be an easy way to fix this.
This works perfectly.

1 Like