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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago