Author: Mohammad Mazhar Ansari
In MuleSoft and other java applications we use properties files for configuration. However, since the YAML standard came into the picture industry is moving/preferring YAML over for various reasons. Few of the reasons are mentioned below:
- YAML Spec can be found here
- Human Readable
- Supports key/val, basically map, List and scalar types (int, string etc.)
- Its usage is quite prevalent in many languages like Python, Ruby, and Java
- Hierarchical Structure
- While retrieving the values from .yml file we get the value as whatever the respective type (int, string etc.) is in the configuration
It happens a lot of times that we have a property file and we need to convert it to YAML, as very few tools are available to convert the Property file to YAML and vice versa. I am trying to solve this problem using a dataweave script.
Convert Property File to YAML File
- Take any random property file
led=false
drove=replied
school.twelve=true
school.heart=useful
school.too=-2100386915.9792404
school.gate=true
school.height.dress=has
school.height.buried=146236814
school.height.perhaps=catch
school.height.parallel=typical
school.height.spin=mostly
school.height.my=false
school.mean=false
extra=true
appropriate=602076185.9872131
our=true
- Go to http://34.205.75.56:8081/
- Paste the Content on Left hand side window and change type to TXT
- Paste below dataweave script in middle window
%dw 2.0
import * from dw::core::Strings
output application/yaml
var delimiter = "="
var lineSeparator = "\n"
fun generateArray (str) = str splitBy(lineSeparator)
fun isSubChildExists (str) = ((((str splitBy(delimiter)) [0]) splitBy (".")) [1] != null)
fun propToJSON(str) = if (isSubChildExists(str)) {
(substringBefore(str, ".")) : propToJSON(substringAfter(str, "."))
}
else
(substringBefore(str, delimiter)): substringAfter(str, delimiter)
fun arrToObj(arr) = arr reduce ((env, obj={}) -> obj ++ env)
fun CombineObjBasedOnKey (Obj) =
if (typeOf(Obj) == Array and sizeOf(Obj..) > 1)
((Obj groupBy (item, index) -> keysOf(item)[0]) mapObject ((value, key, index) ->
if (log("ObjectType2: " ++ key, typeOf(value)) == Array)
(key): (log("CombineObjBasedOnKey2: ",CombineObjBasedOnKey(value..'$key')))
else if (log("ObjectType2: " ++ key, typeOf(value)) == String)
value
else
(key): (log("CombineObjBasedOnKey3: ",value))
))
else
Obj[0]
---
CombineObjBasedOnKey(generateArray(payload) map ((item, index) ->
propToJSON(item)
))
- Right hand side windows will have desired output

- Dataweave script have 2 variables delimiter and lineSeparator at line # 3 and 4 respectively. Please change it according to your use case.
Convert YAML File to Property File
- Go to https://onlineyamltools.com/generate-random-yaml to generate a Random YAML file
- Choose Below Options

- Click on Generate a new random configuration file

- Click on Copy to clipboard
- Go to http://34.205.75.56:8081/
- Paste the Content on Left hand side window and change type to YAML
- Paste below dataweave script in middle window
%dw 2.0
output text/plain
var delimiter = "="
var lineSeparator = "\n"
fun generateKey (parentKey, childKey) = (
if (parentKey == "" and childKey == "")
""
else if (parentKey == "" and childKey != "")
childKey
else
parentKey ++ "." ++ childKey
)
fun convertToProperties (parentKey, childKey, childNode) = (
if (typeOf(childNode) == Object )
childNode mapObject( convertToProperties(log('parentKey', generateKey(parentKey, childKey)), log('childKey',$$), log('childNode',$)))
else if (typeOf(childNode) == Array )
(generateKey(parentKey, childKey)): childNode joinBy ","
else (generateKey(parentKey, childKey)): childNode
)
fun convertJSONToArray (root) = (
(root mapObject ( str: "$$" as String ++ delimiter ++ $ as String)).*str
)
---
convertJSONToArray(convertToProperties("", "", payload)) joinBy lineSeparator
- Right hand side window will have desired output

- Dataweave script have 2 variables delimiter and lineSeparator at line # 3 and 4 respectively. Please change it according to your use case.