Property to YAML File Conversion Using MuleSoft

Mazhar Ansari
3 min readApr 20, 2020

--

Note: While copy/paste the code snippet from blog please covert quotes and double quotes.

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 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 lots of times that we have a property file and we need to convert to YAML. as because very less 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) filter ($ != "" and !(startsWith($, "#")))
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 Playground
  • Dataweave script have 2 variables delimiter and lineSeparator at line # 3 and 4 respectively. Please change it according to your use case.

Note: All property files are not compatible to convert to YAML files. e.g.

leisure.locales=en,fr
leisure.locales.en.title=Leisure Page
leisure.locales.fr.title=Page de Loisir

As you can see in our example leisure.locales is a text property however as well as Object if converted to YAML, thats not possible. Hence not compatible.

Convert YAML File to Property File

YAML Configuration Generation Options
  • Click on Generate a new random configuration file
Generate random YAML Configuration
  • 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 windows will have desired output
Dataweave Playground
  • Dataweave script have 2 variables delimiter and lineSeparator at line # 3 and 4 respectively. Please change it according to your use case.

--

--

Mazhar Ansari
Mazhar Ansari

Written by Mazhar Ansari

I am seasoned Integration Architect with around 18+ yrs of exp. I have extensively worked on TIBCO and Mulesoft. Mainly in EAI, ESB, SOA, API and BPM projects.

No responses yet