Git Product home page Git Product logo

dataweave-scripts's Introduction

DataWeave Scripts

Here's a compilation of some of the (fairly complex) DW scripts I've done.

Click on a title to see the function's code, input, and output.

More Info

The screenshots were generated using the DataWeave Playground. To learn more about this, please read this article: How to run locally the DataWeave Playground Docker Image.

For questions, you can contact me here: www.alexandramartinez.world/contact

Table of Contents

Recursive Functions

Tail Recursive Functions

Others

Recursive Functions

Creates a tree from a flat array with parent/child relationship.

Keywords: fun, do, var, as, filter, ~=, ==, if/else, isEmpty, match/case, map, $, orderBy, skipNullOn

Input: Array<Object>

Output: Object

Example:

getChildren recursive function used from the DataWeave Playground

Tail Recursive Functions

Transforms a JSON payload into a different JSON structure and keeps a count of the indexes accross the whole output array. This function has its own repository that contains additional explanations and links to other resources such as slides and previous versions. To learn more about it please go to this repository: Reviewing a Complex DataWeave Transformation Use-case.

Keywords: dw::util::Values, import, update/with, fun, if/else, do, var, ~=, +, to, default, map, $, ++

Input: Object

Output: Array<Object>

Example:

addIndexTailRecursive tail recursive function used from the DataWeave Playground

Count the number of days between two dates using certain filters to either count some days or keep them out.

Filters:

  • includeEndDate - Boolean to include the endDate in count or not. Default value is false.
  • includingDaysOfWeek - Array of Numbers to include just certain days of the week in count. Default is to count all days of the week (1-7).
  • excludingDates - Array of Dates to include certain dates that should not be counted. Default is empty.

Keywords: var, as, fun, do, contains, and, not, if/else, +, >, ==, |P| (Period)

Input: NA

Output: Number

Example:

getDaysBetween tail recursive function used from the DataWeave Playground

Extract values from a JSON input using a String representation of a path.

Keywords: dw::core::Strings, import, isNumeric, substringAfter, fun, do, var, scan, regex, isEmpty, if/else, match/case, as

Input: Object, Array

Output: Whichever value was selected from the input and with the path.

Example:

extractPath tail recursive function used from the DataWeave Playground

Returns the filtered given value using the conditions passed in an Array of Strings.

Keywords: fun, if/else, isEmpty, do, var, splitBy, filter, ~=, to

Input: Array<Object>

Output: Array<Object>

Example:

filterValueByConditions tail recursive function used from the DataWeave Playground

Mixing the previous two functions (extractPath and filterValueByConditions) and adding a bit more code to them, this function extracts a specific path and filters the output depending on the given conditions. This also contains an additional function: isArrayOfArray to check if a given value is of the type Array<Array>.

Note: in order to apply the filters successfully, the given key must be from an Array.

Keywords: dw::core::Strings, import, isNumeric, substringAfter, typeOf, ~=, fun, if/else, isEmpty, do, var, splitBy, filter, $, to, scan, match/case, as

Input: Object, Array

Output: Whichever value was selected from the input and with the path.

Example:

extractPathWithFilters tail recursive function used from the DataWeave Playground

Others

Replaces the value with a masked String when the field or the field's attribute contains private information. This function can also be used for different data types, you just need to remove the first condition since it's no longer reading the XML attributes (fieldsToMask contains value.@name).

Keywords: dw::util::Tree, import, mapLeafValues, var, fun, if/else, or, contains, @

Input: Can be anything, but in this example is XML Object

Output: Same as input

Example:

maskFields and mapLeafValues functions used from the DataWeave Playground

Evaluates if the values from an Array contain at least one empty value (null, [], "", {}). To read more about these 3 different approaches please check out this post: How to check for empty values in an array in DataWeave | Part 4: Arrays Module.

Keywords: dw::core::Arrays, import, some, fun, if/else, isEmpty, $, match/case

Input: Array, Null

Output: Boolean

Example:

containsEmptyValues functions used from the DataWeave Playground

dataweave-scripts's People

Contributors

alexandramartinez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.