Remove empty string from array using JOLT Transform

30 Views Asked by At

I need to write jolt spec for this transformation

Input :

{
  "key": [
    "a",
    "b",
    "c",
    "",
    "d",
    "e",
    "f",
    ""
  ]
}

Output should be :

{
  "key": [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f"
  ]
}

What would be jolt spec for above transformation , basically removing all empty string from array ?

1

There are 1 best solutions below

0
Barbaros Özhan On BEST ANSWER

The trick is to match the "" values with the value null such as "": null :

[
  {
    "operation": "shift",
    "spec": {
      "*": { // the level of the array "key"
        "*": { // the level of the indexes of the array 
          "": null,
          "*": { // the values other than ""s
            "@1": "&3" // @1 replicates the innermost values as a whole
                       // while matches &3 which copies the literal "key"
          }
        }
      }
    }
  }
]

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

enter image description here