Split Arrays item into key:value pairs

Hi, i’ve browsed dozens of forum threads and trying various function but still can’t get it to work. I have the following output from html extract node, I want to convert each items into key:value pairs like:

[
  {
    "name": "Restoran / Kafe",
    "address": "Restoran / Kafe",
    "category": "Restoran / Kafe"
  },
  {
    "name": "Restoran / Kafe",
    "address": "Restoran / Kafe",
    "category": "Restoran / Kafe"
  },
  {
    "name": "Restoran / Kafe",
    "address": "Restoran / Kafe",
    "category": "Restoran / Kafe"
  }
]

the output data that i need to convert:

[
  {
    "name": [
      "CHAPAYOM",
      "Restoran The Kampong Cili Padi",
      "Mr Plate Cafe"
    ],
    "address": [
      "S-G-38, Maxim Citylights Sentul, No. 25, Jalan Sentul Perdana, , WP Kuala Lumpur W.P Kuala Lumpur",
      "NO 38, G-1, JALAN MEDAN PB 1, SEKSYEN 9, ,43650 BANDAR BARU BANGI, ,SELANGOR DARUL EHSAN, , Hulu Langat Selangor",
      "No 15, Susur Jalan Kluang/ Pusat Perniagaan Parit Raja 6400, 86400 Parit Raja, Johor, , Batu Pahat Johor"
    ],
    "category": [
      "Restoran / Kafe",
      "Restoran / Kafe",
      "Restoran / Kafe"
    ],
    "tempoh": [
      "17 Feb Hingga - 23 Mar 2023",
      "17 Feb Hingga - 17 May 2023",
      "19 Feb Hingga - 19 Feb 2023"
    ]
  }
]

Hi @lamanrasmi, if you are looking for a no-code solution you could split up your individual fields, then merge the results like so:

This is the result:

I’ll also post an alternative code solution in my next comment.

1 Like

And here’s the promised code solution which is a bit shorter:

Result:

I’ve prepared both examples on n8n 0.215.2 since you didn’t specify which version of n8n you have in use.

Hope this helps :slight_smile:

1 Like

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