Home > other >  JOLT spec to remove an array element based on string match
JOLT spec to remove an array element based on string match

Time:09-20

I have below json as input and I want to write jolt spec to remove element which has "TemplateProcessGroupName": "BBBB" and other elements should be present.

[
  {
    "TemplateProcessGroupName": "AAAAA",
    "Name": "Keytab Credential Service"
  },
  {
    "TemplateProcessGroupName": "BBBB",
    "Name": "Keytab Credential Service"
  },
  {
    "TemplateProcessGroupName": "CCCCC",
    "Name": "Keytab Credential Service"
  }
]

Any help would be appreciated. Thanks in advance

Thanks Mahendra

CodePudding user response:

If only needed to remove the attribute "TemplateProcessGroupName" whenever its value is "BBBB", then use the following shift transformation spec :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "TemplateProcessGroupName": {
          "BBBB": { 
            "*": "" //display nothing whenever the attribute matches "BBBB"
          },
          "*": {
            "@1": "[&3].&2" // "[&3]" stands for going the tree three levels up and grab the outermost indexes and yielding result of an array
          }
        },
        "*": "[&1].&"
      }
    }
  }
]

If the whole object which contains that attribute should be removed then use shift transformation specs along with a remove transformation spec such as :

[
  {
   // change the object labels
    "operation": "shift",
    "spec": {
      "*": {
        "@": "@(1,TemplateProcessGroupName)"
      }
    }
  },
  {
   // remove the object with label "BBBB"
    "operation": "remove",
    "spec": {
      "BBBB": ""
    }
  },
  {
   // get rid of object labels
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]
  • Related