String manipulation (creating unique one) in Dataweave

Viewed 44

Needed some quick help with Dataweave. For this input properties:

  1. "OrderNumber": "99995105" Create random order number every time, with same length (8). I think we can use random() function for it.

  2. "OrderDate": "2022-09-12T11:55:26.000+00:00". This needs to be unique every time. So use now() function, but the date should be converted to the format above.

  3. "ShipmentNumber": "99995105-00026002-flytekiq". This needs to be unique as well, but in similar format. So we can cut the first number(99995105), and replace it with random number of same length (8)? And then reconstruct the entire ShipmentNumber in original format.

  4. "LineItemNumber": "99995105-0e579bab784639a79423701109-flytekiq" Exact same as #3

Thank you!!

1 Answers

As mentioned in some thread questions, this is not bulletproof, but for a testing scenario it may produce something useful enough.

%dw 2.0
output application/json

// Using random
var rand1 = randomInt(99999999)

// Using another random strategy
var epoc = now() as Number
var uuidNumbers = uuid() replace  /[a-z-]*/ with ""
var rand2 = (epoc * uuidNumbers) mod 100000000

// You can try with rand1 or rand2, but both will create collides at some point
var orderNumber = rand1 as String {format: "00000000"}
---
{
    "OrderNumber": orderNumber,
    "OrderDate": now() as String {format: "yyyy-MM-dd'T'HH:mm:ss.SSSxxx"}
    "OrderDate": "2022-09-12T11:55:26.000+00:00",
    "ShipmentNumber": orderNumber ++ "-00026002-flytekiq",
    "LineItemNumber": orderNumber ++ "-0e579bab784639a79423701109-flytekiq"
}

EDIT: As @Harshank suggested, I added the timezone to the format. Thanks!

Related