Search code examples
jsonjolt

Jolt: Concat of three stings using join in jolt


we are using the current jolt to concat two strings, when we have more added, how to add in sequence manner

input:

[
  {
    "policy": "Name",
    "PAFrstNm": "John"
  },
  {
    "PALastNm": "Jammy",
    "policy": "Name"
  },
  {
    "PAMiNm": "T",
    "policy": "Name"
  },
  {
    "policy": "Name",
    "PPFrstNm": "sam"
  },
  {
    "policy": "Name",
    "PPLastNm": "jim"
  },
  {
    "policy": "Name",
    "PPMiNm": "E"
  },
  {
    "policy": "Name",
    "PPFrstNm1": "jun"
  },
  {
    "PPLastNm1": "san",
    "policy": "Name"
  },
  {
    "PPMiNm1": "e",
    "policy": "Name"
  }
]

current Jolt:

[
  { // 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
        }
      }
    }
  }
]

Expected Output: PAfullnam: First +Mi +Lastname

[
  {
    "PAfullnam": "John T Jammy",
    "policy": "Name"
  },
  {
    "PPfullnam": "sam E jim ",
    "policy": "Name"
  },
  {
    "PPfullnam1": "jun e san ",
    "policy": "Name"
  }
]

Current output:

[
  {
    "PAfullnam": "John Jammy T",
    "policy": "Name"
  },
  {
    "PPfullnam": "sam jim E",
    "policy": "Name"
  },
  {
    "PPfullnam1": "jun san e",
    "policy": "Name"
  }
]

Firstname + last name


Solution

  • You can add this one as the second spec

      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "0|2|1": "&2.&1.[#1]"
            }
          }
        }
      }
    

    in order to sort those values by their indexes within the formed arrays. So, use the following spec:

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*LastN*|*FrstN*|*MiN*": "@1,policy.&(0,1)fullna&(0,2)"
              // the order of the part above is not important
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "0|2|1": "&2.&1.[#1]"
            }
          }
        }
      },
      { 
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*ful*": "=join(' ',@(1,&))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "[#2].&",
              "$1": "[#2].policy" 
            }
          }
        }
      }
    ]