DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Trending

  • Microsoft Fabric Throttling: Diagnose Before You Scale
  • Azure Key Vault Secrets That Reconcile Themselves in Go, With mamori
  • Kubernetes Operations Playbook: The Essentials for Keeping Scale, Complexity, and Drift Under Control
  • Android Application Security: Android Application Architecture

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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 
Gaurav Dubey
·
Updated Mar. 06, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
9.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook