Understanding read and readUrl Functions With MuleSoft DataWeave
A few input and output examples to highlight the differences between the read and readUrl functions in MuleSoft DataWeave.
Join the DZone community and get the full member experience.
Join For Freeread Function
read function is used to read the string or binary and returned parsed content. It is a very useful function when the reader isn't able to determine the content type by default.
It takes three parameters:
- stringToParse — Binary or String payload.
- contentType — Supported Format.
- readerProperties — Sets configuration properties and it is optional.
Example 1
In this example, inputData
is in string format and using the read
function, we can parse into CSV format.
Here is the DataWeave transformation:
%dw 2.0
output application/json
var inputData=
"name,age,salary
Joseph,34,3000
James,32,5000"
---
read(inputData,"application/csv")
It will generate the below output in the JSON format:
xxxxxxxxxx
[
{
"name": "Joseph",
"age": "34",
"salary": "3000"
},
{
"name": "James",
"age": "32",
"salary": "5000"
}
]
Example 2
In this example, inputData
is in string format without header, and using theread
function we are parsing into CSV format, and mentioned the configuration properties as header=false.
Here is the DataWeave transformation.
xxxxxxxxxx
%dw 2.0
output application/json
var inputData=
"Joseph,34,3000
James,32,5000"
---
read(inputData,"application/csv",{header:false})
It will generate the below output in the JSON format:
xxxxxxxxxx
[
{
"column_0": "Joseph",
"column_1": "34",
"column_2": "3000"
},
{
"column_0": "James",
"column_1": "32",
"column_2": "5000"
}
]
In the above output, we noticed that it automatically add column names as keys to the output object.
readURL Function
ThereadUrl
function is similar to the read function, but it accepts URL as an input and the remaining parameters remain the same as the read function.
It takes three parameters:
- url — URL string to read.
- contentType — Supported Format.
- readerProperties — Sets configuration properties and it is optional.
Example 1
In this example, the readUrl function will read the data from the HTTP URL.
Here is the DataWeave transformation:
xxxxxxxxxx
%dw 2.0
output application/json
---
readUrl("https://jsonplaceholder.typicode.com/users/5","application/json")
Output:
xxxxxxxxxx
{
"id": 5,
"name": "Chelsey Dietrich",
"username": "Kamren",
"email": "Lucio_Hettinger@annie.ca",
"address": {
"street": "Skiles Walks",
"suite": "Suite 351",
"city": "Roscoeview",
"zipcode": "33263",
"geo": {
"lat": "-31.8129",
"lng": "62.5342"
}
},
"phone": "(254)954-1289",
"website": "demarco.info",
"company": {
"name": "Keebler LLC",
"catchPhrase": "User-centric fault-tolerant solution",
"bs": "revolutionize end-to-end systems"
}
}
Now, we want to write DataWeave transformation for reading the name, username, and email instead of returning full JSON payload.
Here is the DataWeave transformation:
xxxxxxxxxx
%dw 2.0
output application/json
var inputData=readUrl("https://jsonplaceholder.typicode.com/users/5","application/json")
---
{
name: inputData.name,
username: inputData.username,
email: inputData.email
}
Output:
xxxxxxxxxx
{
"name": "Chelsey Dietrich",
"username": "Kamren",
"email": "Lucio_Hettinger@annie.ca"
}
Example 2
In this example, we will be reading input from the file employee.json file located at the classpath (src/main/resources).
Here is the DataWeave transformation:
xxxxxxxxxx
%dw 2.0
output application/json
---
readUrl("classpath://employee.json","application/json")
Output:
x
[
{
"id": "1",
"firstName": "Tom",
"lastName": "Cruise",
"age": 25,
"salary": 20000
},
{
"id": "2",
"firstName": "Maria",
"lastName": "Sharapova",
"age": 28,
"salary": 10000
},
{
"id": "3",
"firstName": "James",
"lastName": "Bond",
"age": 32,
"salary": 30000
}
]
Now, you know how and when to use the read and readUrl functions in the DataWeave transformation.
Opinions expressed by DZone contributors are their own.
Comments