# Transform complex structures to array of objects

**URL:** <https://forum.alumio.com/t/transform-complex-structures-to-array-of-objects/965>\
**Category:** Data Transformers\
**Created:** [February 7, 2024, 1:58pm UTC](https://forum.alumio.com/t/transform-complex-structures-to-array-of-objects/965 "2024-02-07T13:58:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniellefers](https://forum.alumio.com/user_avatar/forum.alumio.com/daniellefers/32/1563_2.png) [@daniellefers](https://forum.alumio.com/u/daniellefers)\
**Post date:** [February 7, 2024, 1:58pm UTC](https://forum.alumio.com/t/transform-complex-structures-to-array-of-objects/965/1 "2024-02-07T13:58:23Z")

</div>

Hi,

I would like to transform complex structures to an simple array with objects.

In the dataset below, I want each value in `values.*.*.data` to be an new entry in the result array, combined with keys and values from their parent object.

**Input:**

```auto
{
    "values": {
        "image_type_1": [
            {
                "locale": "en_US",
                "data": [
                    "image_type1_en_1.jpg",
                    "image_type1_en_2.jpg"
                ]
            }
        ],
        "image_type_2": [
            {
                "locale": "en_US",
                "data": [
                    "image_type2_en_1.jpg",
                    "image_type2_en_2.jpg"
                ]
            },
            {
                "locale": "nl_NL",
                "data": [
                    "image_type2_nl_1.jpg",
                    "image_type2_nl_2.jpg"
                ]
            }
        ]
    }
}

```

**Desired output:**

```auto
{
    "result": [
        {
            "type": "image_type_1",
            "locale": "en_US",
            "asset": "image_type1_en_1.jpg"
        },
        {
            "type": "image_type_1",
            "locale": "en_US",
            "asset": "image_type1_en_2.jpg"
        },
        {
            "type": "image_type_2",
            "locale": "en_US",
            "asset": "image_type2_en_1.jpg"
        },
        {
            "type": "image_type_2",
            "locale": "en_US",
            "asset": "image_type2_en_2.jpg"
        },
        {
            "type": "image_type_2",
            "locale": "nl_NL",
            "asset": "image_type2_nl_1.jpg"
        },
        {
            "type": "image_type_2",
            "locale": "nl_NL",
            "asset": "image_type2_nl_2.jpg"
        },
    ]
}

```

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![Gugi](https://forum.alumio.com/user_avatar/forum.alumio.com/gugi/32/236_2.png) [@Gugi](https://forum.alumio.com/u/Gugi)\
**Post date:** [February 12, 2024, 3:14pm UTC](https://forum.alumio.com/t/transform-complex-structures-to-array-of-objects/965/2 "2024-02-12T15:14:28Z")

</div>

Hi @daniellefers

There are a few steps need to be done to achieve your objective.

First, as it will require a chain of multiple entity transformers, you should put [Chain multiple entity transformers](https://forum.alumio.com/t/chain-multiple-entity-transformers/256) as the root of your entity transformer.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/f/f6ecae3fc72ddf8671f5381fb9d85ad2717de8c4.png)

Next, as you can see that the image types (`image_type_1` and `image_type_2`) are keys in the associative array `values`. In order to move it to a value, you should use [Move data between accessors](https://forum.alumio.com/t/move-data-between-accessors/284).

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/0/055fd63d147acf8cee83bbf6c8946a8ce53a44af.png)

Based on the above transformer, the image types are now a value of `type` key and the previous value will be set as a value of `data` property.

```auto
{
  "values": [
    {
      "type": "image_type_1",
      "data": [
        {
          "locale": "en_US",
          "data": [
            "image_type1_en_1.jpg",
            "image_type1_en_2.jpg"
          ]
        }
      ]
    },
    {
      "type": "image_type_2",
      "data": [
        {
          "locale": "en_US",
          "data": [
            "image_type2_en_1.jpg",
            "image_type2_en_2.jpg"
          ]
        },
        {
          "locale": "nl_NL",
          "data": [
            "image_type2_nl_1.jpg",
            "image_type2_nl_2.jpg"
          ]
        }
      ]
    }
  ]
}

```

Then, you should transform the `data` array that contains the image filenames into an array of objects. You can use [Move using a pattern](https://forum.alumio.com/t/move-using-a-pattern/285) to do so.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/2/2cb9d266a07e59d278b7eadbfe9920cc9d56cfbb.png)

It will result in the below entity, where each image filename is now a value of key `asset` in each object of the array.

```auto
{
  "values": [
    {
      "type": "image_type_1",
      "data": [
        {
          "locale": "en_US",
          "data": [
            {
              "asset": "image_type1_en_1.jpg"
            },
            {
              "asset": "image_type1_en_2.jpg"
            }
          ]
        }
      ]
    },
    {
      "type": "image_type_2",
      "data": [
        {
          "locale": "en_US",
          "data": [
            {
              "asset": "image_type2_en_1.jpg"
            },
            {
              "asset": "image_type2_en_2.jpg"
            }
          ]
        },
        {
          "locale": "nl_NL",
          "data": [
            {
              "asset": "image_type2_nl_1.jpg"
            },
            {
              "asset": "image_type2_nl_2.jpg"
            }
          ]
        }
      ]
    }
  ]
}

```

You should now copy the image type to each asset object we created above. Since you have more than one image type in each object of array `values`, and you may have more in the actual case, you should use [Node, transform nodes](https://forum.alumio.com/t/node-transformer/348) to loop into the `values` array. Then, use [Recursively copy values to children](https://forum.alumio.com/t/recursively-copy-values-to-children/287).

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/a/a2a4753b2c5099a83f327cf49d31cab327ef08ab.png)

Now, each object that contains asset (image filename) will have the image type as well.

```auto
[
  {
    "values": [
      {
        "type": "image_type_1",
        "data": [
          {
            "locale": "en_US",
            "data": [
              {
                "asset": "image_type1_en_1.jpg",
                "type": "image_type_1"
              },
              {
                "asset": "image_type1_en_2.jpg",
                "type": "image_type_1"
              }
            ]
          }
        ]
      },
      {
        "type": "image_type_2",
        "data": [
          {
            "locale": "en_US",
            "data": [
              {
                "asset": "image_type2_en_1.jpg",
                "type": "image_type_2"
              },
              {
                "asset": "image_type2_en_2.jpg",
                "type": "image_type_2"
              }
            ]
          },
          {
            "locale": "nl_NL",
            "data": [
              {
                "asset": "image_type2_nl_1.jpg",
                "type": "image_type_2"
              },
              {
                "asset": "image_type2_nl_2.jpg",
                "type": "image_type_2"
              }
            ]
          }
        ]
      }
    ]
  }
]

```

The remaining value to be copied is `locale`, which requires the same way as the previous one but with different pattern of the [Node, transform nodes](https://forum.alumio.com/t/node-transformer/348) and the pattern of the Recursively copy values to children.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/d/dde66d3fff9360ce3fedd52040f5f8c87f5b192f.png)

Then, you will have all the data you need in each object.

```auto
{
  "values": [
    {
      "type": "image_type_1",
      "data": [
        {
          "locale": "en_US",
          "data": [
            {
              "asset": "image_type1_en_1.jpg",
              "type": "image_type_1",
              "locale": "en_US"
            },
            {
              "asset": "image_type1_en_2.jpg",
              "type": "image_type_1",
              "locale": "en_US"
            }
          ]
        }
      ]
    },
    {
      "type": "image_type_2",
      "data": [
        {
          "locale": "en_US",
          "data": [
            {
              "asset": "image_type2_en_1.jpg",
              "type": "image_type_2",
              "locale": "en_US"
            },
            {
              "asset": "image_type2_en_2.jpg",
              "type": "image_type_2",
              "locale": "en_US"
            }
          ]
        },
        {
          "locale": "nl_NL",
          "data": [
            {
              "asset": "image_type2_nl_1.jpg",
              "type": "image_type_2",
              "locale": "nl_NL"
            },
            {
              "asset": "image_type2_nl_2.jpg",
              "type": "image_type_2",
              "locale": "nl_NL"
            }
          ]
        }
      ]
    }
  ]
}

```

However, since you need all the objects merged into an array, you will need a help from JMESPath to do so.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/2X/7/71b104117bfe61048d2fcb987552d52bb6f496f8.png)

Finally, you will have a new property called `result` that contains the array of objects you wants to achieve.

```auto
{
  "result": [
    {
      "asset": "image_type1_en_1.jpg",
      "type": "image_type_1",
      "locale": "en_US"
    },
    {
      "asset": "image_type1_en_2.jpg",
      "type": "image_type_1",
      "locale": "en_US"
    },
    {
      "asset": "image_type2_en_1.jpg",
      "type": "image_type_2",
      "locale": "en_US"
    },
    {
      "asset": "image_type2_en_2.jpg",
      "type": "image_type_2",
      "locale": "en_US"
    },
    {
      "asset": "image_type2_nl_1.jpg",
      "type": "image_type_2",
      "locale": "nl_NL"
    },
    {
      "asset": "image_type2_nl_2.jpg",
      "type": "image_type_2",
      "locale": "nl_NL"
    }
  ]
}

```

We hope our explanation is clear to you. Please give it a try and let us know if you find any issues.
