JOLT Transform issue with multiple array

44 Views Asked by At

I have the below Jolt input specification. where i need the output in a different format

{
  "EBody": {
    "ServiceDetails": {
      "LineDetail": {
        "LineNumber": [
          "0",
          1
        ],
        "Description": [
          "detail 1",
          "A205330410780"
        ],
        "Amount": [
          "1",
          "2.00"
        ],
        "Unit price": [
          "45.00",
          "2,181.66"
        ],
        "VAT Rate": "0.00%",
        "SurchargeDiscount": {
          "Type": "SC",
          "Percentage": "20.00%",
          "Amount": "872.66"
        },
        "Total Price": "3,490.66"
      }
    }
  }
}

Expected output is:

{
  "EBody": {
    "ServiceDetails": {
      "LinesDetail": [
        {
          "LineNumber": "0",
          "Quantity": "1",
          "Unit Price": "45.00"
        },
        {
          "LineNumber": 1,
          "VAT Rate": "0.00%",
          "Quantity": "2.00",
          "Unit Price": "2,181.66",
          "SurchargeDiscount": {
            "Type": "SC",
            "Percentage": "20.00%",
            "Amount": "872.66"
          }
        }
      ]
    }
  }
}

please help me with this jolt specification. I had tried with some specs which is not giving accurate result

1

There are 1 best solutions below

1
On BEST ANSWER

You can use the following shift transformation spec :

[
  {
    "operation": "shift",
    "spec": {
      "*": { // the level of "EBody"
        "*": { // the level of "ServiceDetails"
          "*D*": { // the level of "LineDetail"
            "LineNumber|Unit price": {
              "*": {
                "@": "&5.&4.&(3,1)sD&(3,2)[&1].&2"
                        // ^here add "s" between "Line" and "Detail"
              }
            },
            "Amount": { // rename Amount to Quantity
              "*": {
                "@": "&5.&4.&(3,1)sD&(3,2)[&1].Quantity"
              }
            },
            "VAT Rate|SurchargeDiscount": "&3.&2.&(1,1)sD&(1,2)[1].&"
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is :

enter image description here