Categories: Dataweave

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

Recent Posts

Find Intersection of Two Singly Linked Lists

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

1 month ago

Minimum Cost to Paint Houses with K Colors

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

2 months ago

Longest Absolute Path in File System Representation

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

2 months ago

Efficient Order Log Storage

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

3 months ago

Select a Random Element from a Stream

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

3 months ago

Estimate π Using Monte Carlo Method

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

3 months ago