Search code examples
arraysjsonmappingtransformationjolt

Rename Json field "LVL2" to "ORG_LVL_1" in JOLT


Rename Json field "LVL2" to "ORG_LVL_1" in JOLT

[
  {
    "LVL2": "Co0053",
    "status": "Active"
  },
  {
    "LVL2": "Co0054",
    "status": "Active"
  }
]

Desired Output where ORG_LVL_2 is the modified/renamed JSON field which was previously LVL2 :

[
  {
    "ORG_LVL_2": "Co0053",
    "status": "Active"
  },
  {
    "ORG_LVL_2": "Co0054",
    "status": "Active"
  }
]

Solution

  • You can use either of the following transformation specs :

    1.

    [
     { // generate new attribute, namely "ORG_LVL_2"
       "operation": "modify-overwrite-beta",
       "spec": {
         "*": {
           "ORG_LVL_2": "@(1,LVL2)"
         }
       }
     },
     { // delete the original attribute
       "operation": "remove",
       "spec": {
         "*": {
           "LVL2": ""
         }
       }
     }  
    ]
    

    or

    2.

    [
     {
       "operation": "shift",
       "spec": {
         "*": {
           "*2": "[&1].ORG_&(0,1)_2", // use &(0,1) representation technique >to replicate the substring LVL; where 0 : the current level, 1 : the first >asterisk(indeed there's only single one)
           "*": "[&1].&" // keep the other attribute fixed
         }
       }
     }
    ]