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"
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
}
]
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
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
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…