Convert Alphanumeric Text to JSON with Dataweave | MuleSoft Guide

This post will teach you how to use dataweave to convert an alphanumeric text into a JSON object. We may alter data in a variety of ways with the help of Dataweave, a potent scripting language. As an example, we’ll utilize the dataweave script that follows: 

%dw 2.0import * from dw::core::Stringsvar num = flatten(payload filter ((character, index) -> character matches /[0-9]/) scan /[0-9]{2}/)// scan /[0-9]{2}/ it will divide the string into array and each item must have size 2var char = flatten(payload filter ((character, index) -> character matches /[a-zA-Z]/) scan /[a-zA-Z]{3}/)output application/json  ---// scan /[a-zA-Z]{3}/ it will divide the string into array and each item must have size 3{  (num map (    ($): char[$$]  ))}

The payload of this script is a string of alphanumeric characters, and its output is a JSON object. These actions are performed by the script:  

  •  In order to use the helpful functions for working with strings, it imports the Strings module from the dw::core library.  
  •  Two variables are declared, num and char. The filter and scan functions are used to extract an array of two-digit numbers from the payload, which is then stored in the num variable. Using a lambda expression, the filter function determines if each letter in the payload matches the regular expression /[0–9]/, which denotes any digit between 0 and 9. Another regular expression, /[0–9]{2}/, which denotes any two consecutive numbers, is passed to the scan function, which returns an array of matches. By using the flatten function, the nested array is reduced to a single array. The num variable will store [12, 34, 56] for instance, if the payload is “a12b34c56d”.  
  •  Using different regular expressions, but the same logic as the num variable, the char variable contains an array of three-letter words that were taken from the payload. Every character is compared to the regular expression /[a-zA-Z]/, which denotes every letter from A to Z, capital or lowercase, via the filter function. An array of matches is returned by the scan function when it receives the regular phrase /[a-zA-Z]{3}/, which denotes any three consecutive letters. The char variable will hold [“abc”] if the payload is, for instance, “a12b34c56d”.  
  •  The map function and the curly braces {} are then used by the script to output a JSON object. Each entry in the num array is mapped by the map function, which accepts a lambda expression, to a key-value pair in the JSON object. The value is the appropriate element in the char array at the same index, and the key is the element itself. For instance, the output JSON object will be {“12”: “abc”, “34”: null, and “56”: null} if the char array is [“abc”] and the num array is [12, 34, 56].  

 With just a few lines of code, we can quickly convert an alphanumeric string into a JSON object by utilizing dataweave. Numerous other features and capabilities that Dataweave provides enable us to work with data in a variety of circumstances and formats.

One minor issue with the script mentioned above is that it ignores any extra single numbers or extra single or double alphabets that appear in the string, as seen in the example below.

Therefore, to address this scenario, we must make a slight change to the script above. Replace scan /[0–9]{2}/ with scan /[0–9]{1,2}/ and replace scan /[a-zA-Z]{3}/ with scan /[a-zA-Z]{1,3}/. 

%dw 2.0import * from dw::core::Stringsvar num = flatten(payload filter ((character, index) -> character matches /[0-9]/) scan /[0-9]{1,2}/)// scan /[0-9]{1,2}/ it will divide the string into array and each item have size 2 or 1var char = flatten(payload filter ((character, index) -> character matches /[a-zA-Z]/) scan /[a-zA-Z]{1,3}/)output application/json  ---// scan /[a-zA-Z]{3}/ it will divide the string into array and each item have size 3, 2 or 1{  (num map (    ($): char[$$]  ))}

Example:

Input

“01020304056INDAUSENGUSACHNUK”

Output

{"01": "IND","02": "AUS","03": "ENG","04": "USA","05": "CHN","6": "UK"}

Note:

 I used the code above to demonstrate how to utilize the matches function, but we can also accomplish this without it by using the code below.  

]]>
Post a Comment (0)
Previous Post Next Post