-3

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!!

5
  • You mean only 99995105 part of #3 and #4 needs to be randomized? Sep 16, 2022 at 19:12
  • Random doesn't guarantee uniqueness. You are putting several 'questions' into one. They are not even questions. You should abstract them into one more generic question with maybe a couple of variants. We are not here to code for you. Questions should be useful for others two. In that vein, 4 should be removed completely since it is the same as 3), right? No reason to ask twice. Please rethink the question.
    – aled
    Sep 16, 2022 at 19:30
  • Not to mention #2 is a basic "Date to string" conversion in Dataweave which I am pretty sure you can find it easily at this point. Sep 16, 2022 at 19:38
  • For 1) is there any information from that input that is useful or it can just be ignored? if the later, just remove it from the question. 2) to what above format? You didn't mention a format. 3) similar format to what? You should provide the definition of what you are trying to achieve. 4) should be removed as commented. For all you should provide example inputs, outputs and the logic for the transformations. 4) just remove it.
    – aled
    Sep 16, 2022 at 19:38
  • @HarshankBansal, yes only 99995105 needs to be randomized. This is for demo purpose only, so we should be good with Random. I will remove #4. I tried that earlier, but somehow could not find option to edit question. Will look. Sep 16, 2022 at 21:03

1 Answer 1

0

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!

2
  • 1
    Try lowercase "xxx" for timezone. That will give you +00:00 Sep 16, 2022 at 20:54
  • You can update the script to add regex to take the input string and replace the first occurence of [d] and - with the random number as string. Something like: (inpString replace /^([\d+]*\-)/ with (randomInt(100000000) as String ++ "-"))
    – Salim Khan
    Sep 16, 2022 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.