Lutz
Lutz

Reputation: 29

Truncate every string in an array with JOLT Transformation

I try to truncate strings in an array with JOLT. The character "^" should be removed from any string that contains it in the first position.

Sample Input:

{
  "scores": [
    "^aaaa",
    "^bbbb",
    "cccc",
    "^dddd",
    "eeee"
  ]
}

Expected Output:

{
  "scores" : [ "aaaa", "bbbb", "cccc", "dddd", "eeee" ]
}

My Spec:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "splittedScores": "=split('\\^',@(1,scores))",
      "scores": "=join('',@(1,splittedScores))" // <--- Not working
    }
  }
]

Upvotes: 0

Views: 308

Answers (1)

Barbaros &#214;zhan
Barbaros &#214;zhan

Reputation: 65323

Seems you'd need one extra shift transformation sandwiched between modify transformations to tame the subarrays such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=split('\\^',@(1,&))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=join('',@(1,&))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "scores"
      }
    }
  }
]

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

enter image description here

or only one shift transformation will be sufficient to use(a straightforward option) such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "^*": {
            "$(0,1)": "&3"
          },
          "*": {
            "$": "&3"
          }
        }
      }
    }
  }
]

splitting by "$(0,1)" wildcard for the object under "^*" node. &3 represents going three levels up to grab the name of the key scores .

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

enter image description here

If ^ is being removed in the first position really matters, then the second method; if all ^ characters from any strings should be removed, then the first method should be used.

Upvotes: 1

Related Questions