jsontransformationspecificationsjolt

Transform JSON using jolt transformation


I have the input JSON

{
  "MainObj": {
    "SubObject": {
      "Invoices": [
        {
          "InvoiceItemsDetail": [
            {
              "InvoiceItemLineNumber": "1",
              "ItemCode": "122",
              "Description": "GoodsDescription1"
            },
            {
              "InvoiceItemLineNumber": "2",
              "ItemCode": "222",
              "Description": "GoodsDescription2"
            }
          ]
        },
        {
          "InvoiceItemsDetail": [
            {
              "InvoiceItemLineNumber": "3",
              "ItemCode": "322",
              "Description": "GoodsDescription3"
            },
            {
              "InvoiceItemLineNumber": "4",
              "ItemCode": "422",
              "Description": "GoodsDescription4"
            }
          ]
        }
      ]
    }
  }
}

I require the result which does not contain "AnotherObject" and "SomeOtherObject".

The resultant JSON will contain

all of these inside an array of "outputs"

{
  "outputs": [
    {
      "INVOICE_LINE_ITEM_NUMBER": "1",
      "ITEM_CODE": "122",
      "DESCRIPTION": "GoodsDescription1"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "2",
      "ITEM_CODE": "222",
      "DESCRIPTION": "GoodsDescription2"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "3",
      "ITEM_CODE": "322",
      "DESCRIPTION": "GoodsDescription3"
    },
    {
      "INVOICE_LINE_ITEM_NUMBER": "4",
      "ITEM_CODE": "422",
      "DESCRIPTION": "GoodsDescription4"
    }
  ]
}

Solution

  • You just need "*": "[]" key-value pair as the innermost part of the transformation such as

    [
      {
        "operation": "shift",
        "spec": {
          "MainObj": {
            "SubObject": {
              "Invoices": {
                "*": {
                  "InvoiceItemsDetail": {
                    "*": "[]"
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    the demo on the site Jolt Transform Demo Using v0.1.1 is :

    enter image description here

    EDIT : Rather you can use the following spec, considering that you need to rename the attributes for the new edited case :

    [
      { //determine the objects seperated by upper indexes separately
        "operation": "shift",
        "spec": {
          "MainObj": {
            "SubObject": {
              "Invoices": {
                "*": {
                  "InvoiceItemsDetail": {
                    "*": {
                      "Invoice*": "&3_&1.INVOICE_LINE_ITEM_NUMBER",
                      "Item*": "&3_&1.ITEM_CODE",
                      "Desc*": "&3_&1.DESCRIPTION"
                    }
                  }
                }
              }
            }
          }
        }
      },
      { //get rid of the object keys while adding an array wrapper, namely "outputs"
        "operation": "shift",
        "spec": {
          "*": "outputs[]"
        }
      }
    ]