readUrl Example in DataWeave

  • February 6, 2022
  • Dataweave

readUrl Example

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

Input

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

dw script

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

OutPut

"Somebody"
Example

In this example, we will be reading input from the file employee.json file located at the classpath (src/main/resources).

Source

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

Output

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

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.

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

dw script

%dw 2.0
var myJsonSnippet = readUrl("classpath://myJsonSnippet.json", "application/json")
output application/csv
---
(myJsonSnippet.results map(item) -> item.profile)

Output

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

Note: Also read about https://coderzpy.com/readurl-in-dataweave

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 *