Combining / moving objects in an array based on product number

Hi there,

I am trying to combine / move objects from a storage based on (a part of) the product number.

For example, this is my data:

{
  "products": {
    "1234_A01_P01": {
      "data": "productdata"
    },
    "1234_A02_P02": {
      "data": "productdata"
    },
    "1235_A02_TX": {
      "data": "productdata"
    },
    "1352_A06_PP": {
      "data": "productdata"
    },
    "1352_A01_PP": {
      "data": "productdata"
    }
  }
}

I want to create objects based on the part before the first β€œ_” and combine / move the existing objects into a nested one. The output should be this:

{
  "1234": {
    "1234_A01_P01": {
      "data": "productdata"
    },
    "1234_A02_P02": {
      "data": "productdata"
    }
  },
  "1235": {
    "1235_A02_TX": {
      "data": "productdata"
    }
  },
  "1352": {
    "1352_A06_PP": {
      "data": "productdata"
    },
    "1352_A01_PP": {
      "data": "productdata"
    }
  }
}

This way, all the objects that correspond to the first values before the β€œ_” are inside the same object.

Hi @j.wiegmann welcome,

Yes, we have dealt with this type of logic.
The solution to this one is a bit more complicated to explain directly…
Long story short, you copy they keys inside themselves and format them accordingly.
In you case, regex before the first β€œ_” to get the β€œ1234” and so on.
After that group them by the new result from above.
Final result in Alumio looks like this

Transformer would be of much use to you to actually see the logic
export_20240223104512.ndjson (2.4 KB)

1 Like

Thanks @catalin, I imported the transformer and this works perfectly.