Search code examples
jsonapache-nifijolt

Removing square brackets in JOLT


I have the jolt input below :

{
  "pnum": "4545",
  "clascode": null,
  "retun": "NN",
  "skcmm": [
    "AA",
    "EE",
    "KN",
    "NH",
    "NL"
  ]
}

My current spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "&"
    }
  },
  {
    "operation": "modify-default-beta",
    "spec": {
      "SMS_SPMFORMAT": "${spm_format}",
      "SMS_PRODUCTIONFORMAT": "${partNumberConverted}"
    }
  }
]

The current output:

{
  "pnum" : "4545",
  "clascode" : null,
  "retun" : "NN",
  "skcmm" : [ "AA", "EE", "KN", "NH", "NL" ],
  "SMS_SPMFORMAT" : "${spm_format}",
  "SMS_PRODUCTIONFORMAT" : "${partNumberConverted}"
}

But the expected output is :

{
  "pnum" : "4545",
  "clascode" : null,
  "retun" : "NN",
  "skcmm" : "AA,EE,KN,NH,NL",
  "SMS_SPMFORMAT" : "${spm_format}",
  "SMS_PRODUCTIONFORMAT" : "${partNumberConverted}"
}

I need to remove the square brackets for the lists and add it as string with double quotes. Can you please help here.


Solution

  • You can use a join function for skcmm after converting the modify transformation from default to overwrite in order to overwrite the existing value of skcmm such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "SMS_SPMFORMAT": "${spm_format}",
          "SMS_PRODUCTIONFORMAT": "${partNumberConverted}",
          "skcmm": "=join(',',@(1,&))" // concatenates the components of the array comma-separatedly
        }
      }
    ]
    

    Btw, using the existing shift transformation, which replicates exactly the whole content, is redundant.

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

    enter image description here