# Transform nested array of hashes (dry-transformer)

**URL:** <https://discourse.dry-rb.org/t/transform-nested-array-of-hashes-dry-transformer/1056>\
**Category:** Support\
**Created:** [May 20, 2020, 12:44pm UTC](https://discourse.dry-rb.org/t/transform-nested-array-of-hashes-dry-transformer/1056 "2020-05-20T12:44:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miral](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Miral](https://discourse.dry-rb.org/u/Miral)\
**Post date:** [May 20, 2020, 12:44pm UTC](https://discourse.dry-rb.org/t/transform-nested-array-of-hashes-dry-transformer/1056/1 "2020-05-20T12:44:18Z")

</div>

Hi,

Getting to grips with dry-transformer, using it to convert my ActiveRecord objects into a hash for an API call I’m going to make. Seems to do what I need apart from when things get a bit more complex.

This is my current input into a mapper class I’ve created

```
{ id: 1, user_id: 2, line_items: [{order_id: 1, desc: "hello"}, {order_id: 1, desc: "world"}] }

```

In my mapper i’m renaming the `id` field to `ShipmentId` and `user_id` to `CustomerId`.

Now I want to transform the array of `line_items`. I want to rename them to `ShipmentLines` (easy) and I want to go over each hash in the array and rename `order_id` to `ShipmentId` (finding this difficult).

Here’s my current mapper class:

```
define! do
  deep_symbolize_keys
  accept_keys(ACCEPTED_KEYS)
  rename_keys id: :ShipmentId
  rename_keys user_id: :CustomerId
  rename_keys line_items: :ShipmentLines
end

```

I wanted to add a `map_array(:ShipmentLines) do end` block in the define block but that doesn’t seem to be working. I get argument errors.

Does anyone know how to transform a hash which also has a key with an array of hashes (`line_items` in this case)? Thanks!

**Edit:** Got it working by separating it into a new mapper class, not sure if this is the correct way to do it, or if there’s a better way.

```
map_value :ShipmentLines, -> lines do
  lines.map { |line_item| ShipmentLines.new.(line_item) }
end
```

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [May 22, 2020, 10:51am UTC](https://discourse.dry-rb.org/t/transform-nested-array-of-hashes-dry-transformer/1056/2 "2020-05-22T10:51:19Z")

</div>

I believe this would work:

```ruby
map_value :ShipmentLines do
  map_array do
    # do your stuff
  end
end

```

---

<div class="post-metadata">

**Author:** ![Miral](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Miral](https://discourse.dry-rb.org/u/Miral)\
**Post date:** [May 22, 2020, 11:51am UTC](https://discourse.dry-rb.org/t/transform-nested-array-of-hashes-dry-transformer/1056/3 "2020-05-22T11:51:42Z")

</div>

Thanks will give it a go.
