%dw 2.0
output application/json
---
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
readUrl
readUrl(url: String, contentType: String = "application/dw", readerProperties: 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 Supported Data Formats. |
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.)