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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

1 week ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

3 weeks ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

6 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

6 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

7 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

7 months ago