I am working with the Toggl API and it is returning a JSON object containing data like so (edited to simplify)
{
"user_id": 12345,
"username": "JohnSmith",
"description": "foo",
"time_entries": [
{
"id": 3778920070,
"seconds": 10800,
"start": "2025-01-27T11:00:00+00:00",
"stop": "2025-01-27T14:00:00+00:00",
"at": "2025-01-27T16:17:24+00:00",
"at_tz": "2025-01-27T16:17:24+00:00"
}
],
"row_number": 1
},
{
"user_id": 6789,
"username": "JaneSmith",
"description": "bar",
"time_entries": [
{
"id": 3778684944,
"seconds": 5977,
"start": "2025-01-27T14:35:31+00:00",
"stop": "2025-01-27T16:15:08+00:00",
"at": "2025-01-27T16:15:09+00:00",
"at_tz": "2025-01-27T16:15:09+00:00"
}
],
"row_number": 2
},
{
"user_id": 12345,
"username": "JohnSmith",
"description": "bar",
"time_entries": [
{
"id": 3780521038,
"seconds": 3600,
"start": "2025-01-27T09:00:00+00:00",
"stop": "2025-01-27T10:00:00+00:00",
"at": "2025-01-28T13:34:31+00:00",
"at_tz": "2025-01-28T13:34:31+00:00"
}
],
"row_number": 3
},
I would like to read the array and copy a subsection of the data into a new array. Ideally the output would group matching usernames, for example
{
"username": "JohnSmith",
"description": "foo",
"time_entries": [
{
"id": 3778920070,
"seconds": 10800,
"start": "2025-01-27T11:00:00+00:00",
"stop": "2025-01-27T14:00:00+00:00",
"at": "2025-01-27T16:17:24+00:00",
"at_tz": "2025-01-27T16:17:24+00:00"
},
{
"id": 3780521038,
"seconds": 3600,
"start": "2025-01-27T09:00:00+00:00",
"stop": "2025-01-27T10:00:00+00:00",
"at": "2025-01-28T13:34:31+00:00",
"at_tz": "2025-01-28T13:34:31+00:00"
}
],
],
"row_number": 1
},
{
"user_id": 6789,
"username": "JaneSmith",
"description": "bar",
"time_entries": [
{
"id": 3778684944,
"seconds": 5977,
"start": "2025-01-27T14:35:31+00:00",
"stop": "2025-01-27T16:15:08+00:00",
"at": "2025-01-27T16:15:09+00:00",
"at_tz": "2025-01-27T16:15:09+00:00"
}
],
"row_number": 2
},
{
In pseudo-code, I am thinking I should do something like
foreach element in array if key = username , push to new array
if already exist only copy the time_entries key/value under that username
Is there a more elegant approach ?