Search code examples
jsonapache-nifijolt

JOLT Transformation --- Adding Header From The Element


I have the JSON object below :

{
  "trans-details": [
    {
      "customernum": "41700013",
      "crmordernum": "SO-000048949",
      "customerordernum": "Over delivery BRAZI",
      "itemnumber": "A0002309953",
      "status": 3,
      "pagecount": 64,
      "orderstatus": 3,
      "requestedqty": 0
    },
    {
      "customernum": "41700013",
      "crmordernum": "SO-000049948",
      "customerordernum": "Over delivery test ZA",
      "itemnumber": "A0002309911",
      "status": 3,
      "pagecount": 64,
      "orderstatus": 3,
      "requestedqty": 0
    }
  ]
}

Expected output :

{
  "Header": {
    "Pagenumber": "1",
    "Countryid": "7002",
    "First Page": "true",
    "Last Page": "false",
    "countryId": 675,
    "dealerNumber": 675000,
    "Total Pages": "64" //(PAGE COUNT)
  },
  "TRANS-DETAILS": [
    {
      "customernum": "41700013",
      "crmordernum": "SO-000048949",
      "customerordernum": "Over delivery BRAZI",
      "itemnumber": "A0002309953",
      "status": 3,
      "orderstatus": 3,
      "requestedqty": 0
    },
    {
      "customernum": "41700013",
      "crmordernum": "SO-000048949",
      "customerordernum": "Over delivery BRAZI",
      "itemnumber": "A0002309953",
      "status": 3,
      "orderstatus": 3,
      "requestedqty": 0
    }
  ]
}

The "PAGECOUNT:64" should only show in the header part, not in the element.Please help. Thanks


Solution

  • You can use the following transformation specs

    [
      { // generate an extra attribute to represent the key of the array in order 
        // to be able to process within the next specs 
        "operation": "shift",
        "spec": {
          "*": {
            "$": "ttl",
            "@": "&"
          }
        }
      },
      { // get the uppercase title
        "operation": "modify-overwrite-beta",
        "spec": {
          "ttl": "=toUpper(@(1,&))"
        }
      },
      { // replace of the array's title with uppercase form of it
        "operation": "shift",
        "spec": {
          "tr*": "@1,ttl"
        }
      },
      { // take pagecount to an outer object, namely "Header"
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "pagecount": "Header.Total Pages",
              "*": {
                "@": "&3[&2].&1"
              }
            }
          }
        }
      },
      { // pick only one(the first) Total Pages value 
        "operation": "cardinality",
        "spec": {
          "Header": {
            "*": "ONE"
          }
        }
      },
      { // add the attributes with fixed values
        "operation": "modify-overwrite-beta",
        "spec": {
          "Header": {
            "Pagenumber": "1",
            "Countryid": "7002",
            "First Page": "true",
            "Last Page": "false",
            "countryId": 675,
            "dealerNumber": 675000
          }
        }
      },
      { // sort the objects in alphabetical order
        "operation": "sort"
      }
    ]