Search code examples
jsonjolt

Jolt: concat of continues key values at the end


I have one requirement in jolt to concat based on the key values.. key value may get increased. like PPfullname,PPfullname1... PPfullname2...

input :

[
  {
    "policy": "Name",
    "PAFrstNm": "John"
  },
  {
    "PALastNm": "Jammy",
    "policy": "Name"
  },
  {
    "policy": "Name",
    "PPFrstNm": "sam"
  },
  {
    "policy": "Name",
    "PPLastNm": "jim"
  },
  {
    "policy": "Name",
    "PPFrstNm1": "jun"
  },
  {
    "PPLastNm1": "san",
    "policy": "Name"
  }
]

expected output:

[
  {
    "PAfullname": "John Jammy",
    "policy": "Name"
  },
  {
    "PPfullname": "sam jim",
    "policy": "Name"
  },
  {
    "PPfullname1": "jun san",
    "policy": "Name"
  }
]

Solution

  • You can use the following spec

    [
      { // group by LastN/FrstN removed key names 
        "operation": "shift",
        "spec": {
          "*": {
            "*LastN*": {
              "@": "@2,policy.&(1,1)fullna&(1,2)"
            },
            "*FrstN*": {
              "@": "@2,policy.&(1,1)fullna&(1,2)"
            }
          }
        }
      },
      { // concatenate the components of the arrays respectively 
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*ful*": "=join(' ',@(1,&))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "[#2].&",
              "$1": "[#2].policy" // deliver back the policy attribute to those objects
            }
          }
        }
      }
    ]