Flattening Toggl API response

Describe the issue/error/question

I’m trying to flatten some nested JSON data from Toggl’s API response. If I split the items out, I achieve my desired outcome, but losing all the other fields that I want to retain. If I choose to retain all the fields, then the array stays nested.


Please share the workflow

Share the output returned by the last node

[
  {
    {
      "user_id": 2734320,
      "username": "--------------------",
      "project_id": 174158675,
      "task_id": null,
      "billable": false,
      "description": "-----------------------",
      "tag_ids": [
        10350720
      ],
      "billable_amount_in_cents": null,
      "hourly_rate_in_cents": null,
      "currency": "USD",
      "row_number": 1,
      "time_entries": {
        "id": 2069990493,
        "seconds": 3600,
        "start": "2021-06-28T08:00:00+01:00",
        "stop": "2021-06-28T09:00:00+01:00",
        "at": "2022-01-03T18:10:14+00:00"
      }
    },
]

Information on your n8n setup

  • n8n version: 0.218.0
  • Database you’re using (default: SQLite): MariaDB
  • Running n8n with the execution process [own(default), main]:???
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]: Home Assistant OS addon, self-hosted

Hey @Chaoscontrol,

Can you share the output from Toggl and an example of what you would want that output to look like?

This is a single item (the output is just 50 of these):

[
  {
    "user_id": 2734320,
    "username": "redacted",
    "project_id": 174158675,
    "task_id": null,
    "billable": false,
    "description": "redacted",
    "tag_ids": [
      10350720
    ],
    "billable_amount_in_cents": null,
    "hourly_rate_in_cents": null,
    "currency": "USD",
    "time_entries": [
      {
        "id": 2069990493,
        "seconds": 3600,
        "start": "2021-06-28T08:00:00+01:00",
        "stop": "2021-06-28T09:00:00+01:00",
        "at": "2022-01-03T18:10:14+00:00"
      }
    ],
    "row_number": 1
  }
]

This is the desired:

[
  {
    "user_id": 2734320,
    "username": "redacted",
    "project_id": 174158675,
    "task_id": null,
    "billable": false,
    "description": "redacted",
    "tag_ids": [
      10350720
    ],
    "billable_amount_in_cents": null,
    "hourly_rate_in_cents": null,
    "currency": "USD",
    "time_entries_id": 2069990493,
    "time_entries_seconds": 3600,
    "time_entries_start": "2021-06-28T08:00:00+01:00",
    "time_entries_stop": "2021-06-28T09:00:00+01:00",
    "time_entries_at": "2022-01-03T18:10:14+00:00",
    "row_number": 1
  }
]

I hope I haven’t messed up the syntax. The keys are not important. I just want to flatten, as there’s never more than 1 time entry per item.

If possible, I’d prefer if this wasn’t resolved with JS, but using some node set up. As I’m not too familiar with JS, and this is something that I will stumble upon frequently in the flows I need to build. So I’m looking to understand the logic and right way of doing it, more than this specific case scenario.

Many thanks.

Hey @Chaoscontrol,

Best I can do without using any code at all would be the below, First we change the time_entries array to an object (which might be enough for you at this point) but then to make it more like your output we use the Set node to recreate the data structure.

It’s a good solution, thank you very much.

Would it be too much to ask to see what would be the code alternative? If it’s quite basic I might keep it since I can see how manual this one is.

The end goal is for me to output the table to a Google Spreadsheet, so I believe it’s not enough for me to have it as an object, right?

I think with an object you would be ok if you defined the fields as you can set them by dragging the field into the value.

If you were using the automap option you would need to have the data sorted correctly before you tried to add it.

If it isn’t too busy tomorrow I will do a code node example as well

1 Like

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