Categories: Dataweave

readUrl in 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

Recent Posts

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 days ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago