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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Revolutionizing Scaled Agile Frameworks with AI, MuleSoft, and AWS: An Insider’s Perspective
  • Unlocking the Potential: Integrating AI-Driven Insights with MuleSoft and AWS for Scalable Enterprise Solutions
  • Mastering Multi-Cloud Integration: SAFe 5.0, MuleSoft, and AWS - A Personal Journey
  • AI-Generated DataWeave in MuleSoft: Production Failure Modes and How to Make It Safe

Trending

  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch
  • Spec-Driven Integration: Turning API Sprawl Into a Governed Capability Fleet for AI
  • The Third Culture: Blending Teams With Different Management Models
  • DevOps Is Dead, Long Live Platform Engineering
  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
13.6K 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

  • Revolutionizing Scaled Agile Frameworks with AI, MuleSoft, and AWS: An Insider’s Perspective
  • Unlocking the Potential: Integrating AI-Driven Insights with MuleSoft and AWS for Scalable Enterprise Solutions
  • Mastering Multi-Cloud Integration: SAFe 5.0, MuleSoft, and AWS - A Personal Journey
  • AI-Generated DataWeave in MuleSoft: Production Failure Modes and How to Make It Safe

Partner Resources

×

Comments

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

  • 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