readUrl in DataWeave

  • February 6, 2022
  • Dataweave

readUrl

ThereadUrlfunction is similar to the read function, but it accepts URL as an input and the remaining parameters remain the same as the read function.

Parameters

NameDescription
urlThe URL string to read. It also accepts a classpath-based URL. 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.
contentTypeA supported format (or MIME type). Default: application/dw.
readerPropertiesOptional: Sets reader configuration properties. For other formats and reader configuration properties, see Supported Data Formats.

Example

In this example, the readUrl function will read the data from the HTTP URL.

Source

%dw 2.0
output application/json
---
readUrl("https://jsonplaceholder.typicode.com/users/5","application/json")

Output

{
"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-centricfault-tolerantsolution",
"bs":
"revolutionizeend-to-end systems"
22
  }
23
}

Now, we want to write DataWeave transformation for reading the name, username, and email instead of returning full JSON payload.

Source

%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

{
  "name": "Chelsey Dietrich",
  "username": "Kamren",
  "email": "Lucio_Hettinger@annie.ca"
}

Note: Also read about C Program – Structure & Keywords in C.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Reply

Your email address will not be published. Required fields are marked *