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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • 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
  • Generic and Dynamic API: MuleSoft

Trending

  • Distributed Consensus: Paxos vs. Raft and Modern Implementations
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • A Guide to Auto-Tagging and Lineage Tracking With OpenMetadata
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. MuleSoft DataWeave: Reading From the File

MuleSoft DataWeave: Reading From the File

Follow along with this brief tutorial to follow an example of how to read data from files using MuleSoft DataWeave.

By 
Arpan K Sharma user avatar
Arpan K Sharma
·
Jan. 20, 22 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
12.2K Views

Join the DZone community and get the full member experience.

Join For Free

Before we start, let's go over the following:  

  1.  readurl (): It is dw-core-function and returns parsed content from source at given classpath-based url. readurl("classpath-based url","contentType").
  2. dynamic selector[()]: Return the value of expression passed in parentheses of dynamic selector [()].

Example of Reading Data From Files

The file in use here is zipcode.json, which has zip codes for different cities in India. It is available at classpath "/src/main/resources/".

Example 1:

Reading data from a file can be achieved by using readUrl() in DataWeave.

Data in the "zipcode.json" file is as follows:

JSON
 
{

"Indore" : "455001" ,
"Pune" : "411002",
"Bhopal" : "462003",
"Banglore" : "56004"

}

DataWeave Code, to cath parsed value in dwl variable and map it to a field:

JSON
 
%dw 2.0
output application/json
var zipcodes = readUrl("classpath://zipcode.json" , "application/json")
---
{
	"usingVar" : zipcodes,
	"directlyUsingURL" : readUrl("classpath://zipcode.json" , "application/json")
}

Output:

JSON
 
{
    "usingVar": {
        "Indore": "455001",
        "Pune": "411002",
        "Bhopal": "462003",
        "Banglore": "56004"
    },
    "directlyUsingURL": {
        "Indore": "455001",
        "Pune": "411002",
        "Bhopal": "462003",
        "Banglore": "56004"
    }
}

Example 2:

Mapping a field in response from parsed data of the file can be achieved in a single transform node using "readUrl()" and "dynamic selector [()] for single value".

Data in the "zipcode.json" file: 

JSON
 
{

"Indore" : "455001" ,
"Pune" : "411002",
"Bhopal" : "462003",
"Banglore" : "56004"

}

Input payload:

JSON
 
[
    {
        "city": "Indore"
    },
    {
        "city": "Bhopal"
    }
]

DataWeave code:

JSON
 
%dw 2.0
output application/json
var zipcodes = readUrl("classpath://zipcode.json" , "application/json")
---
payload map ((item, index) -> {
  "City": item.city,
  "Zipcode": zipcodes[item.city]
})

Output:

JSON
 
[
    {
        "City": "Indore",
        "Zipcode": "455001"
    },
    {
        "City": "Bhopal",
        "Zipcode": "462003"
    }
]

Thanks!

MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • 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
  • Generic and Dynamic API: MuleSoft

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!