Search code examples
jsontransformationjolt

How to transform nexted object with nested array in jolt transformation


Input :

[
  {
    "MAIN": {
      "SCALE": "N",
      "TAX": "INV",
      "DETAIL_GROUP": [
        {
          "ORDER_GROUP": "LD6",
          "CENTRAL_TP": [
            {
              "LEG_DETAILS": [
                {
                  "DETAIL_LINE": [
                    {
                      "ORDER_LINE": "L6",
                      "DETAILS": [
                        {
                          "AMOUNT": 1,
                          "QTY": 1,
                          "REGION": "EMEA"
                        }
                      ]
                    },
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 2,
                          "QTY": 2,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                },
                {
                  "DETAIL_LINE": [
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 3,
                          "QTY": 3,
                          "REGION": "EMEA"
                        }
                      ]
                    },
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 4,
                          "QTY": 4,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                },
                {
                  "DETAIL_LINE": [
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 5,
                          "QTY": 5,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                },
                {
                  "DETAIL_LINE": [
                    {
                      "DETAILS": [
                        {
                          "AMOUNT": 6,
                          "QTY": 6,
                          "REGION": "EMEA"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  }
]

Specs :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@MAIN": {
          "@DETAIL_GROUP": {
            "*": {
              "@ORDER_GROUP": "detail[&2].orderGrouop",
              "@CENTRAL_TP": {
                "*": {
                  "@LEG_DETAILS": {
                    "*": {
                      "@DETAIL_LINE": {
                        "*": {
                          "@ORDER_LINE": "detail[&2].orderLine",
                          "@DETAILS": {
                            "*": {
                              "@AMOUNT": "detail[&2].itemInfo.adjustment",
                              "@QTY": "detail[&2].itemInfo.quantity",
                              "REGION": {
                                "#region": "detail[&4].itemInfo.additionalInfo[0].property",
                                "@": "detail[&4].itemInfo.additionalInfo[0].value"
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Output :

{
  "detail": [
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": [
          1,
          3,
          5,
          6
        ],
        "quantity": [
          1,
          3,
          5,
          6
        ],
        "additionalInfo": [
          {
            "property": [
              "region",
              "region",
              "region",
              "region"
            ],
            "value": [
              "EMEA",
              "EMEA",
              "EMEA",
              "EMEA"
            ]
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": [
          2,
          4
        ],
        "quantity": [
          2,
          4
        ],
        "additionalInfo": [
          {
            "property": [
              "region",
              "region"
            ],
            "value": [
              "EMEA",
              "EMEA"
            ]
          }
        ]
      }
    }
  ]
}

Expected :

{
  "detail": [
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": 1,
        "quantity": 1,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": 2,
        "quantity": 2,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "orderGrouop": "LD6",
      "orderLine": "L6",
      "itemInfo": {
        "adjustment": 3,
        "quantity": 3,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": 4,
        "quantity": 4,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": 5,
        "quantity": 5,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    },
    {
      "itemInfo": {
        "adjustment": 6,
        "quantity": 6,
        "additionalInfo": [
          {
            "property": "region",
            "value": "EMEA"
          }
        ]
      }
    }
  ]
}

Solution

  • It will be expected to add more nodes of identifers( in this case I've used &5_&3 and &6_&4 pertaining to the deepness of the relative element ). Btw, no need to prefix the keys with @ wildcard unless it's a path definition. So, you can use the following transformation :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "@MAIN.DETAIL_GROUP": {
              "*": {
                "CENTRAL_TP": {
                  "*": {
                    "LEG_DETAILS": {
                      "*": {
                        "DETAIL_LINE": {
                          "*": {
                            "DETAILS": {
                              "*": {
                                "@8,ORDER_GROUP": "&5_&3.orderGroup",
                                "@2,ORDER_LINE": "&5_&3.orderLine",
                                "AMOUNT": "&5_&3.itemInfo.adjustment",
                                "QTY": "&5_&3.itemInfo.quantity",
                                "REGION": {
                                  "#region": "&6_&4.itemInfo.additionalInfo[0].property",
                                  "@": "&6_&4.itemInfo.additionalInfo[0].value"
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      },
      { // nest whole content within the "detail" array 
        "operation": "shift",
        "spec": {
          "*": "detail[]"
        }
      }
    ]