Split JSON items output to multiple output by code node

Describe the problem/error/question

I want to split JSON items to multiple output by code node js. Why i want to split because when spreadsheet node take items, the node only read first “sheet id”. I have multiple “sheet id” in my JSON items. So when the spreadsheet node insert the items, only first item will be valid and rest items will insert in wrong sheet. How could i handle this? thanks

Please share your workflow

Share the output returned by the last node


I expected the multiple outputs, not single output JSON items.

Information on your n8n setup

  • **n8n version: 0.208.0
  • **Database (default: SQLite): Postgres
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): n8n cloud
  • **Operating system: Window

Hi @Faizal_Abdul_Hakim_I :wave: I’m not exactly sure I understand what you’re looking for - can you maybe share the expected outcome you’re wanting, either as JSON data or as an example Google sheet?

Oops, i sent the wrong image in the “Output Last Node” section. It’s should be the expected i want. But the way I used to achieve “Expected output” was not very efficient. The code like this :

Can you help me rafactor this?

The second workflow goes well but its looks hardcore. So i create the first workflow to refactor but its going invalid output because the items isn’t grouped by month_year. thanks

Hi @Faizal_Abdul_Hakim_I - in order to try to group this for you (because I think you’re on the right track), can you give me some sample input JSON data that I could use with that first workflow? Your code just might need some small tweaks :slight_smile:

I apologize for the unclear, this is sample of data :

[
  {
    "id": "10",
    "nett_gmv": "268000",
    "month_year": "2023-09",
    "date_taken": "2023-09-13",
    "brand": "Guten Inc",
    "store": "Guten",
    "channel": "Lazada"
  },
  {
    "id": "139",
    "nett_gmv": "51809780",
    "month_year": "2023-09",
    "date_taken": "2023-09-13",
    "brand": "Beauty Of Angel",
    "store": "beautyofangel.id",
    "channel": "Shopee"
  },
  {
    "id": "90",
    "nett_gmv": "79123455",
    "month_year": "2023-09",
    "date_taken": "2023-09-13",
    "brand": "Katakita",
    "store": "Katakita Shopee",
    "channel": "Shopee"
  },
  {
    "id": "14",
    "nett_gmv": "494112172",
    "month_year": "2023-09",
    "date_taken": "2023-09-13",
    "brand": "Kintakun",
    "store": "Kintakun Official",
    "channel": "Tokopedia"
  },
  {
    "id": "19",
    "nett_gmv": "64970380",
    "month_year": "2023-08",
    "date_taken": "2023-09-13",
    "brand": "Kintakun",
    "store": "Kintakun Official",
    "channel": "Tokopedia"
  },
  {
    "id": null,
    "nett_gmv": "1353085",
    "month_year": "2023-08",
    "date_taken": "2023-09-13",
    "brand": "Seduh Pertama",
    "store": "Seduh Pertama Store",
    "channel": "Tokopedia"
  },
  {
    "id": "4",
    "nett_gmv": "950000",
    "month_year": "2023-08",
    "date_taken": "2023-09-13",
    "brand": "SJCAM",
    "store": "SJCAM Official Store",
    "channel": "Blibli"
  },
  {
    "id": "1",
    "nett_gmv": "518171",
    "month_year": "2023-08",
    "date_taken": "2023-09-13",
    "brand": "Afka",
    "store": "Afka Official",
    "channel": "Shopee"
  },
  {
    "id": null,
    "nett_gmv": "101617506",
    "month_year": "2023-07",
    "date_taken": "2023-09-13",
    "brand": "dr Ferihana",
    "store": "dr Ferihana Store",
    "channel": "Shopee"
  },
  {
    "id": null,
    "nett_gmv": "1858132614",
    "month_year": "2023-07",
    "date_taken": "2023-09-13",
    "brand": "Teratu",
    "store": "Teratu Beauty Official Shop",
    "channel": "Shopee"
  }
]

Hi @Faizal_Abdul_Hakim_I :wave: Looking closer into this, there’s two problems here - it looks like you might not be properly closing the loop, and you need to set your batch size to 1.

You can read more about the batch size caveat here: Looping | n8n Docs

And you can close the loop like this:

Can you let me know if those two suggestions help? :slight_smile:

2 Likes

Thanks for your help @EmeraldHerald. Its worked!!!

1 Like

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