cancel
Showing results for 
Search instead for 
Did you mean: 

Mapping complex array of objects

andriyFise
Deputy Chef I
Deputy Chef I

My problem is, that im trying to map data from this structure:

[
    {
        "level1_fk": 1,
        "level2_name": "Main Section One",
        "level3_items": [
            {
                "level3_name": "Sub-detail A.1"
            },
            {
                "level3_name": "Sub-detail A.2"
            }
        ]
    }
 
]

To this:
[
    {
        "level1_fk": 1,
        "level2_name": "Main Section One",
        "level3_name": "Sub-detail A.1"   
    },
    {
         "level1_fk": 1,
          "level2_name": "Main Section One",
          "level3_name": "Sub-detail A.2"
     }
]

So the main task to map data, and it must create the same amount of the same object but with different level3_name as in level3_items.
Main requirements is to solve it with using only one foreach loop and Mapper by Workato with some pre-defined Schema

Please help as soon as it possible,


Here is full input array:
[
    {
        "level1_fk": 1,
        "level2_name": "Main Section One",
        "level3_items": [
            {
                "level3_name": "Sub-detail A.1"
            },
            {
                "level3_name": "Sub-detail A.2"
            }
        ]
    },
    {
        "level1_fk": 1,
        "level2_name": "Additional Section One A",
        "level3_items": [
            {
                "level3_name": "Extra A.1"
            }
        ]
    },
    {
        "level1_fk": 1,
        "level2_name": "Additional Section One B",
        "level3_items": [
            {
                "level3_name": "Extra A.2"
            },
            {
                "level3_name": "Extra A.3"
            }
        ]
    },
    {
        "level1_fk": 2,
        "level2_name": "Main Section Two",
        "level3_items": [
            {
                "level3_name": "Sub-detail B.1"
            },
            {
                "level3_name": "Sub-detail B.2"
            },
            {
                "level3_name": "Sub-detail B.3"
            }
        ]
    },
    {
        "level1_fk": 2,
        "level2_name": "More Section Two",
        "level3_items": [
            {
                "level3_name": "Extra B.1"
            },
            {
                "level3_name": "Extra B.2"
            }
        ]
    },
    {
        "level1_fk": 3,
        "level2_name": "Main Section Three",
        "level3_items": [
            {
                "level3_name": "Sub-detail C.1"
            }
        ]
    },
    {
        "level1_fk": 3,
        "level2_name": "Further Section Three X",
        "level3_items": [
            {
                "level3_name": "Extra C.1"
            }
        ]
    },
    {
        "level1_fk": 3,
        "level2_name": "Further Section Three Y",
        "level3_items": [
            {
                "level3_name": "Extra C.2"
            },
            {
                "level3_name": "Extra C.3"
            }
        ]
    },
    {
        "level1_fk": 3,
        "level2_name": "Further Section Three Z",
        "level3_items": [
            {
                "level3_name": "Extra C.4"
            }
        ]
    }
]
1 ACCEPTED SOLUTION

gary1
Star Chef I
Star Chef I

I don't know of a task-efficient way to do this with loops. I would map it using a single Ruby action.

This code snippet works:

merged = []

input["data"].each{|obj| 
  obj["level3_items"].each{|item| 
    
    merged.push(
      {
          "level1_fk": obj["level1_fk"],
          "level2_name": obj["level2_name"],
          "level3_name": item["level3_name"]
      }
    )
  }
}

{merged: merged}

 

Using your full input, it creates this output:

{
    "output": {
        "merged": [
            {
                "level1_fk": 1,
                "level2_name": "Main Section One",
                "level3_name": "Sub-detail A.1"
            },
            {
                "level1_fk": 1,
                "level2_name": "Main Section One",
                "level3_name": "Sub-detail A.2"
            },
            {
                "level1_fk": 1,
                "level2_name": "Additional Section One A",
                "level3_name": "Extra A.1"
            },
            {
                "level1_fk": 1,
                "level2_name": "Additional Section One B",
                "level3_name": "Extra A.2"
            },
            {
                "level1_fk": 1,
                "level2_name": "Additional Section One B",
                "level3_name": "Extra A.3"
            },
            {
                "level1_fk": 2,
                "level2_name": "Main Section Two",
                "level3_name": "Sub-detail B.1"
            },
            {
                "level1_fk": 2,
                "level2_name": "Main Section Two",
                "level3_name": "Sub-detail B.2"
            },
            {
                "level1_fk": 2,
                "level2_name": "Main Section Two",
                "level3_name": "Sub-detail B.3"
            },
            {
                "level1_fk": 2,
                "level2_name": "More Section Two",
                "level3_name": "Extra B.1"
            },
            {
                "level1_fk": 2,
                "level2_name": "More Section Two",
                "level3_name": "Extra B.2"
            },
            {
                "level1_fk": 3,
                "level2_name": "Main Section Three",
                "level3_name": "Sub-detail C.1"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three X",
                "level3_name": "Extra C.1"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three Y",
                "level3_name": "Extra C.2"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three Y",
                "level3_name": "Extra C.3"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three Z",
                "level3_name": "Extra C.4"
            }
        ],
        "log": ""
    }
}

P.S. I edited the above after my initial reply to tweak it a little.

View solution in original post

1 REPLY 1

gary1
Star Chef I
Star Chef I

I don't know of a task-efficient way to do this with loops. I would map it using a single Ruby action.

This code snippet works:

merged = []

input["data"].each{|obj| 
  obj["level3_items"].each{|item| 
    
    merged.push(
      {
          "level1_fk": obj["level1_fk"],
          "level2_name": obj["level2_name"],
          "level3_name": item["level3_name"]
      }
    )
  }
}

{merged: merged}

 

Using your full input, it creates this output:

{
    "output": {
        "merged": [
            {
                "level1_fk": 1,
                "level2_name": "Main Section One",
                "level3_name": "Sub-detail A.1"
            },
            {
                "level1_fk": 1,
                "level2_name": "Main Section One",
                "level3_name": "Sub-detail A.2"
            },
            {
                "level1_fk": 1,
                "level2_name": "Additional Section One A",
                "level3_name": "Extra A.1"
            },
            {
                "level1_fk": 1,
                "level2_name": "Additional Section One B",
                "level3_name": "Extra A.2"
            },
            {
                "level1_fk": 1,
                "level2_name": "Additional Section One B",
                "level3_name": "Extra A.3"
            },
            {
                "level1_fk": 2,
                "level2_name": "Main Section Two",
                "level3_name": "Sub-detail B.1"
            },
            {
                "level1_fk": 2,
                "level2_name": "Main Section Two",
                "level3_name": "Sub-detail B.2"
            },
            {
                "level1_fk": 2,
                "level2_name": "Main Section Two",
                "level3_name": "Sub-detail B.3"
            },
            {
                "level1_fk": 2,
                "level2_name": "More Section Two",
                "level3_name": "Extra B.1"
            },
            {
                "level1_fk": 2,
                "level2_name": "More Section Two",
                "level3_name": "Extra B.2"
            },
            {
                "level1_fk": 3,
                "level2_name": "Main Section Three",
                "level3_name": "Sub-detail C.1"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three X",
                "level3_name": "Extra C.1"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three Y",
                "level3_name": "Extra C.2"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three Y",
                "level3_name": "Extra C.3"
            },
            {
                "level1_fk": 3,
                "level2_name": "Further Section Three Z",
                "level3_name": "Extra C.4"
            }
        ],
        "log": ""
    }
}

P.S. I edited the above after my initial reply to tweak it a little.