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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Website Hosting with MuleSoft API
  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide

Trending

  • DGS GraphQL and Spring Boot
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Solid Testing Strategies for Salesforce Releases
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Understanding read and readUrl Functions With MuleSoft DataWeave

Understanding read and readUrl Functions With MuleSoft DataWeave

A few input and output examples to highlight the differences between the read and readUrl functions in MuleSoft DataWeave.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Jan. 22, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
20.9K Views

Join the DZone community and get the full member experience.

Join For Free

read Function

read function is used to read the string or binary and returned parsed content. It is a very useful function when the reader isn't able to determine the content type by default.

It takes three parameters:

  • stringToParse — Binary or String payload.
  • contentType — Supported Format.
  • readerProperties — Sets configuration properties and it is optional.

Example 1

In this example, inputData is in string format and using the read function, we can parse into CSV format.

Here is the DataWeave transformation:

JSON
 




x


 
1
%dw 2.0
2
output application/json
3
var inputData=
4
"name,age,salary
5
Joseph,34,3000
6
James,32,5000"
7
---
8
read(inputData,"application/csv")



It will generate the below output in the JSON format:

JSON
 




xxxxxxxxxx
1
12


 
1
[
2
  {
3
    "name": "Joseph",
4
    "age": "34",
5
    "salary": "3000"
6
  },
7
  {
8
    "name": "James",
9
    "age": "32",
10
    "salary": "5000"
11
  }
12
]



Example 2

In this example, inputData is in string format without header, and using thereadfunction we are parsing into CSV format, and mentioned the configuration properties as header=false.

Here is the DataWeave transformation.

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
var inputData=
4
"Joseph,34,3000
5
James,32,5000"
6
---
7
read(inputData,"application/csv",{header:false})



It will generate the below output in the JSON format:

JSON
 




xxxxxxxxxx
1
12


 
1
[
2
  {
3
    "column_0": "Joseph",
4
    "column_1": "34",
5
    "column_2": "3000"
6
  },
7
  {
8
    "column_0": "James",
9
    "column_1": "32",
10
    "column_2": "5000"
11
  }
12
]



In the above output, we noticed that it automatically add column names as keys to the output object.

readURL Function

ThereadUrlfunction is similar to the read function, but it accepts URL as an input and the remaining parameters remain the same as the read function.

It takes three parameters:

  • url — URL string to read.
  • contentType — Supported Format.
  • readerProperties — Sets configuration properties and it is optional.

Example 1

In this example, the readUrl function will read the data from the HTTP URL.

Here is the DataWeave transformation:

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
readUrl("https://jsonplaceholder.typicode.com/users/5","application/json")



Output:

JSON
 




xxxxxxxxxx
1
23


 
1
{
2
  "id": 5,
3
  "name": "Chelsey Dietrich",
4
  "username": "Kamren",
5
  "email": "Lucio_Hettinger@annie.ca",
6
  "address": {
7
    "street": "Skiles Walks",
8
    "suite": "Suite 351",
9
    "city": "Roscoeview",
10
    "zipcode": "33263",
11
    "geo": {
12
      "lat": "-31.8129",
13
      "lng": "62.5342"
14
    }
15
  },
16
  "phone": "(254)954-1289",
17
  "website": "demarco.info",
18
  "company": {
19
    "name": "Keebler LLC",
20
    "catchPhrase": "User-centric fault-tolerant solution",
21
    "bs": "revolutionize end-to-end systems"
22
  }
23
}



Now, we want to write DataWeave transformation for reading the name, username, and email instead of returning full JSON payload.

Here is the DataWeave transformation:

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
var inputData=readUrl("https://jsonplaceholder.typicode.com/users/5","application/json")
4
---
5
{
6
    name: inputData.name,
7
    username: inputData.username,
8
    email: inputData.email
9
}



Output:

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "name": "Chelsey Dietrich",
3
  "username": "Kamren",
4
  "email": "Lucio_Hettinger@annie.ca"
5
}



Example 2

In this example, we will be reading input from the file employee.json file located at the classpath (src/main/resources).

Here is the DataWeave transformation:

JSON
 




xxxxxxxxxx
1


 
1
%dw 2.0
2
output application/json
3
---
4
readUrl("classpath://employee.json","application/json")



Output:

JSON
 




x


 
1
[
2
  {
3
    "id": "1",
4
    "firstName": "Tom",
5
    "lastName": "Cruise",
6
    "age": 25,
7
    "salary": 20000
8
  },
9
  {
10
    "id": "2",
11
    "firstName": "Maria",
12
    "lastName": "Sharapova",
13
    "age": 28,
14
    "salary": 10000
15
  },
16
  {
17
    "id": "3",
18
    "firstName": "James",
19
    "lastName": "Bond",
20
    "age": 32,
21
    "salary": 30000
22
  }
23
]



Now, you know how and when to use the read and readUrl functions in the DataWeave transformation.

file IO MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • Website Hosting with MuleSoft API
  • MuleSoft OAuth 2.0 Provider: Password Grant Type
  • Solving Parallel Writing Issues in MuleSoft With Distributed Locking
  • Optimizing MuleSoft Performance With HikariCP: A Complete Guide

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!