---
source: https://carbone.io/documentation/design/repetitions/grouping.html
title: "Group by any item of your data or change the original structure of the JSON data source"
description: "Group by any item of your data or change the original structure of the JSON data source"
generated_at: "2026-07-13"
---

# Grouping

Group by any item of your data or change the original structure of the JSON data source  
COMMUNITY FEATURE Available for:  
✓ Carbone Cloud  
✓ Carbone On-premise  
✓ Embedded Carbone JS   v5.0+  NEW

## Group and sum

A custom iterator can be used to group rows based on the value of a specific attribute (in this case, `brand`).

Additionally, with [aggregator formatters](/documentation/design/computation/aggregation.md), you can sum quantities:

```cdata
[
  { "brand":"Lucid"   , "qty":1  },
  { "brand":"Faraday" , "qty":4  },
  { "brand":"Venturi" , "qty":3  },
  { "brand":"Faraday" , "qty":2  },
  { "brand":"Aptera"  , "qty":1  },
  { "brand":"Lucid"   , "qty":10 }
]
```

**Template:**

| Vehicles | Quantity |
| --- | --- |
| {d[brand].brand} | {d[brand].qty:aggSum(.brand)} |
| {d[brand+1].brand} |   |

**Result:**

| Vehicles | Quantity |
| --- | --- |
| Aptera | 1 |
| Faraday | 6 |
| Lucid | 11 |
| Venturi | 3 |

Get inspired by one of our real-life examples: [Bank Statement](/examples/bank-statement-expert/index.md), [Planning with subtotals](/examples/planification-medium/index.md), [Invoice](/examples/invoice-simple/index.md), or [Store Inventory](/examples/shoes/index.md)

## Transform the result of a database

NEW v5.0.0+

> ℹ️ **Note:** Or since 4.23.0+ with [activated pre-release tag](/documentation/design/overview/version-lifecycle.md#pre-release-features)

Carbone can also transform the JSON with the `:set` formatter. [More information here](/documentation/design/computation/store-and-transform.md).

For example, if the JSON is a flat array coming directly from a database (MySQL, MariaDB, PostgreSQL, Oracle, MS SQL), Carbone can build a hierarchical JSON with just a few lines of code!

```cdata
[
  { "country" : "France", "city" : "Paris" },
  { "country" : "France", "city" : "Nantes" },
  { "country" : "France", "city" : "Pouzauges" },
  { "country" : "Italy" , "city" : "Rome" },
  { "country" : "Italy" , "city" : "Venise" }
]
```

```ctemplate
{d[].city:set(c.countries[id=.country].cities[].name)}
## {c.countries[i].id}
- {c.countries[i].cities[i].name}
- {c.countries[i].cities[i+1].name}
## {c.countries[i+1].id}
```

```cresult
## France
- Paris
- Nantes
- Pouzauges
## Italy
- Rome
- Venise
```

Get inspired by one of our real-life examples: [Gallery](/examples/gallery-simple/index.md)

## Related topics

- [Loop over object properties](/documentation/design/repetitions/with-objects.md)
- [Loop over array](/documentation/design/repetitions/with-arrays.md)
- [Sorting arrays](/documentation/design/repetitions/sorting.md)
- [Lookup](/documentation/design/repetitions/lookup.md)
- [Filtering arrays](/documentation/design/repetitions/filtering.md)
- [Distinct](/documentation/design/repetitions/distinct.md)
