Search code examples
jsonapache-nifijolt

Jolt: Concat of two strings in array


iam trying to concat the two string from the array and have tried the in jolt. i couldn't get out put:

input is:

[
  {
    "policy": "Name",
    "setFrstNm": "John"
  },
  {
    "setLastNm": "Jammy",
    "policy": "Name"
  },
  {
    "policy": "Name",
    "set1FrstNm": "sam"
  },
  {
    "policy": "Name",
    "set1LastNm": "jim"
  },
  {
    "policy": "Name",
    "set2FrstNm1": "jun"
  },
  {
    "set2LastNm1": "san",
    "policy": "Name"
  }
]

Desired output should be:

[
  {
    
    "setfullname": "John jammy"
     "policy": "Name",
  },
  
  {
    "set1fullname": "sam jim",
    "policy": "Name",
  },
  {
    "set2fullname": "jun san",
    "policy": "Name",
  },
  
]

Shift that i am tried for one set

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "*": {
        "setfullname": "=concat(@(1,setFrstNm),' ',@(1,setLastNm))"
        "policy": "Name"
      }
    }
  },

Solution

  • It's important to determine the grouping criteria in order to form upcoming objects will depend on

    [
      { // group by Last/Frs removed key names 
        "operation": "shift",
        "spec": {
          "*": {
            "*Last*": {
              "@": "@2,policy.&(1,1).&(1,1)fullname" // in &(1,1) : the first 1 represents going 1 level up the tree
                                                     //             the second 1 represents the replacement for the 1st asterisk 
            },
            "*Frs*": {
              "@": "@2,policy.&(1,1).&(1,1)fullname"
            }
          }
        }
      },
      { // concatenate the pairs by their key names respectively 
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": "=join(' ',@(1,&))"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*": "[#2].&",
              "$1": "[#2].policy" // deliver back the policy attribute to those objects
            }
          }
        }
      }
    ]
    

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

    enter image description here