VOOZH about

URL: https://dzone.com/articles/dataweave-interview-question-concatenate-elements

⇱ DataWeave Interview Question: Concatenate Elements of an Array


Related

  1. DZone
  2. Coding
  3. Languages
  4. DataWeave Interview Question: Concatenate Elements of an Array

DataWeave Interview Question: Concatenate Elements of an Array

This article will help you practice your DataWeave skills in MuleSoft. We're going to use two different approaches to concatenate an array of letters.

By Updated Mar. 06, 21 · Tutorial
Likes
Comment
Save
8.9K Views

Join the DZone community and get the full member experience.

Join For Free

This article will help you practice your DataWeave skills in MuleSoft. Let's get started.


Input:

JSON




x


1
["m","u","l","e","s","o","f","t"]



Output:

JSON




xxxxxxxxxx
1


1
"mulesoft"



Let's talk about the solution now.

We will apply a reduce function to our input and add the value of the item with the value of the accumulator.

Approach 1 Code:

Java




xxxxxxxxxx
1


1
%dw 2.0
2
output application/json
3
---
4
payload reduce ((item, accumulator) -> accumulator ++ item)



Approach 2 Code:

Java




xxxxxxxxxx
1


1
%dw 2.0
2
output application/json
3
---
4
payload reduce ($$ ++ $)



Here, $$ stands for accumulator and $ stands for an item.

Output:

JSON
x
1
"mulesoft"


If you want reverse output then you can use any of the above approaches with a little modification.

Approach 1 Code:

Java




xxxxxxxxxx
1


1
%dw 2.0
2
output application/json
3
---
4
payload reduce ((item, accumulator) -> item ++ accumulator)



Approach 2 Code:

Java




xxxxxxxxxx
1


1
%dw 2.0
2
output application/json
3
---
4
payload reduce ($ ++ $$)



Output:

JSON




xxxxxxxxxx
1


1
"tfoselum"



Happy Learning!

Data structure Interview (journalism) Element Java (programming language) JSON MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave Interview Question: Compare IDs From Two Arrays and Create a New Array
  • DataWeave Interview Question: Find Unique Names From the Input
  • Dataweave Interview Question Using Map and Reduce
  • Merge Multiple PDFs in MuleSoft

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

Let's be friends: