%dw 2.0
output application/json
---
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
readUrl
DataWeave 2.2 is compatible and bundled with Mule 4.2. This version of Mule reached its End of Life on May 2, 2023, when Extended Support ended. Deployments of new applications to CloudHub that use this version of Mule are no longer allowed. Only in-place updates to applications are permitted. MuleSoft recommends that you upgrade to the latest version of Mule 4 that is in Standard Support so that your applications run with the latest fixes and security enhancements. |
readUrl(String, String, Object): Any
Reads a URL, including a classpath-based URL, and returns parsed content.
This function works similar to the read
function.
The classpath-based URL uses the classpath:`
protocol prefix, for example,
classpath://myfolder/myFile.txt
where myFolder
is located under
src/main/resources
in a Mule project. Other than the URL, readURL
accepts
the same arguments as read
.
Parameters
Name | Description |
---|---|
|
The URL string to read. It also accepts a classpath-based URL. |
|
A supported format (or MIME type). Default: |
|
Optional: Sets reader configuration properties. For other formats and reader configuration properties, see DataWeave Output Formats and Writer Properties. |
Example
This example reads a JSON object from a URL. (For readability, the output
values shown below are shortened with …
.)
Example
This example reads a JSON object from a myJsonSnippet.json
file located in
the src/main/resources
directory in Studio. (Sample JSON content for that
file is shown in the Input section below.) After reading the file contents,
the script transforms selected fields from JSON to CSV. Reading files
in this way can be useful when trying out a DataWeave script on sample data,
especially when the source data is large and your script is complex.
Source
%dw 2.0
var myJsonSnippet = readUrl("classpath://myJsonSnippet.json", "application/json")
output application/csv
---
(myJsonSnippet.results map(item) -> item.profile)
Input
{
"results": [
{
"profile": {
"firstName": "john",
"lastName": "doe",
"email": "johndoe@demo.com"
},
"data": {
"interests": [
{
"language": "English",
"tags": [
"digital-strategy:Digital Strategy",
"innovation:Innovation"
],
"contenttypes": []
}
]
}
},
{
"profile": {
"firstName": "jane",
"lastName": "doe",
"email": "janedoe@demo.com"
},
"data": {
"interests": [
{
"language": "English",
"tags": [
"tax-reform:Tax Reform",
"retail-health:Retail Health"
],
"contenttypes": [
"News",
"Analysis",
"Case studies",
"Press releases"
]
}
]
}
}
]
}
Example
This example reads a CSV file from a URL, sets reader properties to indicate that there’s no header, and then transforms the data to JSON.
Example
This example reads a simple dwl
file from the src/main/resources
directory in Studio, then dynamically reads the value of the key name
from it. (Sample content for the input file is shown in the Input
section below.)