DZone
Integration Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Integration Zone > 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.

Arpan K Sharma user avatar by
Arpan K Sharma
·
Jan. 20, 22 · Integration Zone · Code Snippet
Like (3)
Save
Tweet
4.76K 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.

Popular on DZone

  • OpenTelemetry in Action: Identifying Database Dependencies
  • Why I'm Choosing Pulumi Over Terraform
  • Debugging Deadlocks and Race Conditions
  • Why Great Money Doesn’t Retain Great Devs w/ Stack Overflow, DataStax & Reprise

Comments

Integration Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo