Giter Site home page Giter Site logo

cristianofilho / dataweave-series-for-practice-interview Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 40 KB

Dataweave Series for Practice & Interview - Practice Dataweave here :- Dataweave Playground Important Note:- First Copy Input, Output, DW Code & Use Json Online Validator, to format the JSON, since medium changes the JSON.

Home Page: https://medium.com/@myid535/dataweave-100-solved-practice-interview-problems-275fa654a143

License: MIT License

dataweave mulesoft

dataweave-series-for-practice-interview's Introduction

Dataweave-Series-for-Practice-Interview-Part-1

Dataweave Series for Practice & Interview Part-1 - Practice Dataweave here :- Dataweave Playground Important Note:- First Copy Input, Output, DW Code & Use Json Online Validator, to format the JSON, since medium changes the JSON.

# Dataweave Code Snippets

## 1. Filter Odd & Even Numbers from an Array

**Input:**

[0,1,2,3,4,5,6,7,8,9]


**Output:**

```json
{
  "Even": [0,2,4,6,8],
  "Odd": [1,3,5,7,9]
}

Dataweave Code:

{
  "Even": payload filter (($ mod 2)==0),
  "Odd": payload filter !(($ mod 2)==0)
}

2. Interchange Key-Value in an object

Input:

[
 {
 "message1":"Hello World"
 },
 {
 "message2":"Hello Shubham"
 }
]

Output:

[
 {
 "Hello World": "message1"
 },
 {
 "Hello Shubham": "message2"
 }
]

Dataweave Code:

payload map ((item, index) -> 
 item mapObject(v,k,n)-> {
 (v) : k
 }
)

3. Reverse the Words of a Sentence

Input:

{
 "Reverse": "Dataweave 2.0 is Cool"
}

Output:

"Cool is 2.0 Dataweave"

Dataweave Code:

(payload.Reverse splitBy(' '))[-1 to 0] joinBy(' ')

4. Filter an Array by an Objects field Value(e.g., Year)

Input:

[
 {
 "Product":"A",
 "Year":"2020"
 },
 {
 "Product":"B",
 "Year":"2019"
 }
]

Output:

[
 {
 "Product": "A",
 "Year": "2020"
 }
]

Dataweave Code:

payload filter $.Year>2019

5. Filter Even Values out from any array

Input:

[0,1,2,3,4,5,6,7,8,9]

Output:

[0,2,4,6,8]

Dataweave Code:

payload filter isEven($)

6. Print Last x characters of a number or String

Input:

[
 {
 "num":"000147"
 },
 {
 "num":"100297"
 },
]

Output:

[
 {
 "num": "147"
 },
 {
 "num": "297"
 }
]

Dataweave Code:

import last from dw::core::Strings

payload map(v,k)-> (num: v.num last 3)

7. Count the Number of Characters in a String

Input:

{
 "string":"12shubham245fc"
}

Output:

9

Dataweave Code:

import countCharactersBy,isAlpha from dw::core::Strings

payload.string countCharactersBy isAlpha($)

8. Hamming Distance

Input:

[
 {
 "hola":"bola"
},
{
 "chao":"figo"
}
]

Output:

[
 {
 "Hamming Distance": 1
 },
 {
 "Hamming Distance": 3
 }
]

Dataweave Code:

import hammingDistance from dw::core::Strings

(payload map(v,k)->{
 (v mapObject(value,key,index)->{
 "Hamming Distance":key hammingDistance value
 })
})

9. Find Number of Object or Key-Value Pairs in an Array

Input:

[{
 "k1":"v1"
},
{
 "k2":"v2"
}
]

Output:

2

Dataweave Code:

sizeOf(payload)

10. Generate Random Numbers

Input:

[10]

Output:

[89,26,21,77,80,31,24,13,54,55]

Dataweave Code:

(1 to payload[0]) map randomInt(100)

Feel free to let me know if you need any further assistance!

dataweave-series-for-practice-interview's People

Contributors

cristianofilho avatar

Stargazers

 avatar  avatar

Watchers

 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.