Data View – Convert messages via DZone Web Dev

  • Home / Web Development / Data View –…

Data View – Convert messages via DZone Web Dev

DataView is a language of expression designed by Molosoft. Language helps convert incoming payloads to different payloads as needed. Most developers write a transformation script in a transforming message or set up payload components. The language is firmly integrated with the Mulsure runtime engine and is a must-learn for every Molsoft developer.

Some examples for converting input loads into different patterns:

Example 1: After a row is given, we will generate the following output pattern using the input payload.

Input payload:

          [     {"name": "Roger"    },     {"name": "Michael"    },     {"name": "Harris"    }     ]

Required output pattern:

          [  {    "user 1": "Roger"  },   {    "user 2": "Micheal"  },   {    "user 3": "Harris"  }        ]

Change usage:

%dw 2.0 output application/json --- payload.name map ((item, index) -> {   ("user" ++" "++ index+1): item })

Example 2: Two hey given, [1,2,3,4,5,6,7] And [4,5,6,7,1], We will merge into one row without any documentation. [1,2,3,4,5,6,7]:

Change usage:

%dw 2.0 output application/json var str = [1,2,3,4,5,6,7] var str1= [4,5,6,7,1] --- (str ++ str1) distinctBy ((item, index) ->item )

Example 3: From the object, we will remove the “last name” and “first name” key value pair.

Input payload:

{        "first name": "Keith",       "last name": "Peters",        "age": 25        }

Required output pattern:

{ "age": 25
}

Change usage:

%dw 2.0 output application/json --- payload filterObject ((value, key) -> (key as String != "first name") and key as String !="last name")

Example 4: From row [1,2,3,’a’,’b’], Remove 2 and b.

Change usage:

%dw 2.0 output application/json var arr = [1,2,3,'a','b'] --- arr-2-'b'

Example 5: At the bottom JSON, change all names to capitals.

Input payload:

    [    {"name": "Roger"    },    {"name": "Michael"   },    {"name": "Harris"   }    ]

Output pattern:

[ {   "Name": "ROGER" }, {   "Name": "MICHAEL" }, {   "Name": "HARRIS" } ]

Change usage:

%dw 2.0 output application/json fun UpperCase(input_value)=upper(input_value) --- payload map(item, index) -> Name:UpperCase(item.name)

Example 6: Retrieve all user records over 20 years old.

Input payload:

[ {  "name": "Chris Jordan",  "age": "19"
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Mike",  "age": "20"
} ]

Change usage:

%dw 2.0 output application/json --- payload filter ((item, index) -> item.age>20)

Example 7: Retrieve the average age of individuals in the input payload.

Input payload:

[ {  "name": "Chris Jordan",  "age": "19"
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Mike",  "age": "20"
} ]

Change usage:

%dw 2.0 output application/json --- payload filter ((item, index) -> item.age>20) 

Example 8: Arrange people in salaries according to age.

Input payload:

[ {  "name": "Chris Jordan",  "age": "19"
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Mike",  "age": "20"
} ]

Change usage:

%dw 2.0 output application/json --- payload orderBy ($.age)

Note: If we want the data in descending order

%dw 2.0 output application/json --- payload orderBy (-$.age)

Example 9Exclude invalid values ​​from input payload.

Input payload:

[ {  "name": "Chris Jordan",  "age": null
}, {  "name": "Glenn Maxwell",  "age": "24"
}, {  "name": "Lisa",  "age": null
} ]

Change usage:

%dw 2.0 output application/json skipNullOn = 'everywhere' --- payload

Example 10: Extract the file name from the input payload after “/”

{   "filename":  "njnjnjnnjnbhbhjb/ui900jnnjn"
}

Change usage:

%dw 2.0 output application/json import * from dw::core::Strings --- substringAfterLast(payload.filename,"https://dzone.com/")

Example 11: Format the current date and time as “yyyy-MM-dd HH: mm: ss.S”.

Change usage:

%dw 2.0 output application/json --- now() as LocalDateTime as String {format: "yyyy-MM-dd HH:mm:ss.S"} 

In all of the above examples, we loaded the input in JSON formats and tried to convert the input to the desired patterns using DataView. Thanks for reading!

.

Write a Comment

Your email address will not be published. Required fields are marked *

x