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

  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

Trending

  • How Predictive Analytics Became a Key Enabler for the Future of QA
  • The Real Cost of Change in Software Development
  • How Performance Engineers Find and Fix Hidden System Bottlenecks
  • 5 Proven Strategies for Modernizing Legacy .NET Applications

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. 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
·
Jan. 20, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.0K 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

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

  • Code and Connect: MCP + MuleSoft
  • MuleSoft MCP and A2A in Production: What 17 Recipes Reveal
  • MuleSoft IDP: Enhancing Efficiency and Accuracy in Data Extraction
  • From AI Chaos to Control: Building Enterprise-Grade LLM Gateways With MuleSoft Anypoint

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