-1

I need help on Dataweave 2.0 code for the output which I am receiving input as below:

Input - 0000000123456789

Output should be - 0123456789

How can I fix this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ravi
  • 21
  • 2
  • 7
  • 1
    What is the criteria for the transformation? – aled Jan 11 '21 at 13:13
  • 1
    The question is very unclear. What exactly do you want to achieve? Is it removing initial 0's to just one 0? What if the number is 1111111111123456789. Do you want to remove 1's too? Please update the question. – Harshank Bansal Jan 11 '21 at 16:42

3 Answers3

1

You can try the following DataWeave expression:

%dw 2.0
output application/json
var value = "0000000123456789"
fun getLastChars(str, len) = 
  if (str != null and sizeOf(str) >= len) 
    value[(sizeOf(str) - len) to (sizeOf(str)-1)] 
  else str
---
{
    val: getLastChars(value, 10)
}
olamiral
  • 1,296
  • 7
  • 8
0

If the length of your string is not going to change just do this :

%dw 2.0
output application/json
var myString = "0000000123456789"
---
myString[6 to 15]
fpbg
  • 28
  • 3
0

Just as another interesting way to do it, though as a reusable function olamiral's answer is probably the way to go.

%dw 2.0
output application/json
import withMaxSize from dw::core::Strings
---
(payload[-1 to 1] withMaxSize 10)[-1 to 1]
Michael Jones
  • 1,900
  • 5
  • 12