I’ll demonstrate in this article how to create a list of random codes using DataWeave, taking into account each code’s likelihood of occurring. This can be helpful in situations like sampling, testing, and modeling.
The input data
A JSON array of objects, each with a code and a probability, makes up the input data. The likelihood that the code will occur in the output is expressed as a percentage, or probability. Three codes are present in the input data below, for instance: ABC, DEF, and HIJ. ABC should appear more frequently than the other two codes because of its 60% chance.
Input – variable
var fileData = [ { "code": "ABC", "probability": 60 }, { "code": "DEF", "probability": 20 }, { "code": "HIJ", "probability": 20 }]The DataWeave script
Based on the probability of each input code, the DataWeave script below produces a list of 1000 random codes. Several built-in functions, including map, flatten, and orderBy, are used in the script by the dw::core::Arrays module. To force the values to the appropriate type, it additionally employs the as operator.
%dw 2.0import * from dw::core::Arraysvar fileData = [ { "code": "ABC", "probability": 60 }, { "code": "DEF", "probability": 20 }, { "code": "HIJ", "probability": 20 }]var codes = fileData.codevar percentage = fileData.probabilityvar maxreq = 1000var multiply_items = percentage map ((item) -> (((item default 0) * maxreq) / 100) as Number)var total_codes = flatten(multiply_items map ((item, index) -> (0 to item - 1) map ((i) -> codes[index])))output application/json ---multiply_items flatMap (if ($ < 1) []else ((0 to $ - 1) map ((item, index) -> codes[$$]))) orderBy random()The output data
1000 random codes are included in a JSON array that is sorted by a random function as the result data. The likelihood of each code in the input data should be reflected in the output data. For example, ABC ought to show up more frequently than HIJ or DEF. This is an illustration of output data:
["ABC","ABC","HIJ","ABC","DEF","ABC","ABC","ABC","DEF","ABC", ...] Bonus Script
%dw 2.0import * from dw::core::Arraysvar fileData = [ { "code": "ABC", "probability": 60 }, { "code": "DEF", "probability": 20 }, { "code": "HIJ", "probability": 20 }]var maxRequest = 1000output application/json ---fileData flatMap (1 to (($.probability / 100) * maxRequest) map ((item, index) -> $.code)) orderBy random()Conclusion
Using DataWeave, I created a list of random codes according to their likelihood of occurring, as demonstrated in this post. Applications like testing, simulation, and sampling may benefit from this. Data formats including XML, JSON, CSV, and others can be worked with using DataWeave, a robust data transformation language.
I hope that writing this piece was enjoyable and educational for you, as well as for you to read. Regards and thanks for your support and attention!
]]>