Contact Free trial Login

readUrl

readUrl(String, String, Object): Any

Similar to the read function. However, readURL accepts a URL, including a classpath-based URL.

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. Otherwise, readURL accepts the same arguments as read.

Parameters

Name Description

url

The URL string to read. It also accepts a classpath-based URL.

contentType

A supported format (or MIME type). Default: application/dw.

readerProperties

Optional: Sets reader configuration properties. For other formats and reader configuration properties, see DataWeave Output Formats and Writer Properties.

Example

This example reads a JSON object from a URL. (For readability, the output values shown below are shortened with …​.)

Source

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

Output

{ "userId": 1, "id": 1, "title": "sunt aut ...", "body": "quia et ..." }

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"
            ]
          }
        ]
      }
    }
  ]
}

Output

firstName,lastName,email
john,doe,johndoe@demo.com
jane,doe,janedoe@demo.com

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.)

Source

%dw 2.0
output application/json
---
(readUrl("classpath://name.dwl", "application/dw")).firstName

Input

{
  "firstName" : "Somebody",
  "lastName" : "Special"
}

Output

"Somebody"

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.