Reputation: 1015
Hi I am trying to convert the below input to following output.
Input :
{
"id": 1,
"test": [
{
"key1": "value1"
}
],
"insuranceContractLayer": [
{
"id": 1,
"key1": "value1",
"layerInfo": {
"id": 1,
"name": "test",
"key1": "value1"
},
"otherKey": {
"id": 1,
"name": "test",
"key1": "value1"
}
},
{
"id": 2,
"key1": "value2",
"layerInfo": {
"id": 2,
"name": "test2",
"key1": "value2"
},
"otherKey": {
"id": 2,
"name": "test2",
"key1": "value2"
}
}
]
}
And here is the expected output:
{
"id": 1,
"test": [
{
"key1": "value1"
}
],
"insuranceContractLayer": [
{
"id": 1,
"key1": "value1",
"layerInfo__id": 1,
"layerInfo__name": "test",
"layerInfo__key1": "value1",
"otherKey__id": 1,
"otherKey__name": "test",
"otherKey__key1": "value1"
},
{
"id": 2,
"key1": "value2",
"layerInfo__id": 2,
"layerInfo__name": "test2",
"layerInfo__key1": "value2",
"otherKey__id": 2,
"otherKey__name": "test2",
"otherKey__key1": "value2"
}
]
}
So what I want to achieve is whatever object is inside insuranceContractLayer
. It will be flattened and its key will be prefix + "__" + originalKey
I am hitting my head for past 8 hours and I am not able to understand its weird syntax. Here is the code I tried :
%dw 2.0
import flatten from dw::core::Objects
output application/json
fun flattenObject(obj, prefix = "") =
obj match {
case is Object -> flatten(obj mapObject {
(flattenObject($$, if (prefix == "") "" else prefix ++ "__" ++ ($$ as String))) : $
})
else -> {
{ (prefix): obj }
}
}
---
flattenObject(payload)
Here is the error :
Invalid input "match {\n case is Object -> flatten(obj mapObject {\n (flattenObject($$, if (prefix == "") "" else prefix ++ "__" ++ ($$ as String))) : $\n })\n else -> {\n {", expected Namespace (line 5, column 9):
5| obj match { | ... 10| { (prefix): obj }
Location: main (line: 5, column:9)
I have tried many variations and it is still throwing errors. I am trying to run my code on https://dataweave.mulesoft.com/learn/dataweave
I even tried the simplest version
%dw 2.0
output application/json
---
payload mapObject ((value, key, index) ->
if (key == "insuranceContractLayer")
value map ((layerValue, layerIndex) ->
{
"id": layerValue.id,
"key1": layerValue.key1,
"layerInfo__id": layerValue.layerInfo.id,
"layerInfo__name": layerValue.layerInfo.name,
"layerInfo__key1": layerValue.layerInfo.key1,
"otherKey__id": layerValue.otherKey.id,
"otherKey__name": layerValue.otherKey.name,
"otherKey__key1": layerValue.otherKey.key1
}
)
else
value
)
And it is throwing below error:
Cannot coerce Number (1) to Object
4| payload mapObject ((value, key, index) -> | ... 19|
valueTrace: at main::main (line: 4, column: 20)
But still it is throwing error and I have no clue what does it wants. I could have done this with Javascript easily but I need to do this in DataWeave only and I have no clue how to do this. Can someone help me here?
Upvotes: 0
Views: 458
Reputation: 467
Try this. I hope this will be helpfull
Input:
{
"id": 1,
"test": [
{
"key1": "value1"
}
],
"insuranceContractLayer": [
{
"id": 1,
"key1": "value1",
"layerInfo": {
"id": 1,
"name": "test",
"key1": "value1"
},
"otherKey": {
"id": 1,
"name": "test",
"key1": "value1"
}
},
{
"id": 2,
"key1": "value2",
"layerInfo": {
"id": 2,
"name": "test2",
"key1": "value2"
},
"otherKey": {
"id": 2,
"name": "test2",
"key1": "value2"
}
}
]
}
DataWeave Script:
%dw 2.0
output application/json
---
{
id: payload.id,
test: payload.test,
insuranceContractLayer: payload.insuranceContractLayer map ((layer, index) -> {
((layer - "layerInfo" - "otherKey") ++
(("layerInfo" ++ "__id"): layer.layerInfo.id) ++
(("layerInfo" ++ "__name"): layer.layerInfo.name) ++
(("layerInfo" ++ "__key1"): layer.layerInfo.key1) ++
(("otherKey" ++ "__id"): layer.otherKey.id) ++
(("otherKey" ++ "__name"): layer.otherKey.name) ++
(("otherKey" ++ "__key1"): layer.otherKey.key1))
})
}
Output:
{
"id": 1,
"test": [
{
"key1": "value1"
}
],
"insuranceContractLayer": [
{
"id": 1,
"key1": "value1",
"layerInfo__id": 1,
"layerInfo__name": "test",
"layerInfo__key1": "value1",
"otherKey__id": 1,
"otherKey__name": "test",
"otherKey__key1": "value1"
},
{
"id": 2,
"key1": "value2",
"layerInfo__id": 2,
"layerInfo__name": "test2",
"layerInfo__key1": "value2",
"otherKey__id": 2,
"otherKey__name": "test2",
"otherKey__key1": "value2"
}
]
}
Upvotes: 1
Reputation: 25812
Your last script would not have fully worked but should not give that error in my opinion. It is failing to map the value 1
using map(), which should only happen for the key insuranceContractLayer
. Maybe is a bug in the playground. It would not have worked anyway because comparing a key with a String always returns false. You should have converted the key to String first (key as String
).
If just want to map the values in the array without changing the rest of the object you can use the update
operator, which is more readable and clear in the intention, then map the elements of the array.
%dw 2.0
output application/json
---
payload update {
case a at .insuranceContractLayer ->
a map {
"id": $.id,
"key1": $.key1,
"layerInfo__id": $.layerInfo.id,
"layerInfo__name": $.layerInfo.name,
"layerInfo__key1": $.layerInfo.key1,
"otherKey__id": $.otherKey.id,
"otherKey__name": $.otherKey.name,
"otherKey__key1": $.otherKey.key1
}
}
Output:
{
"id": 1,
"test": [
{
"key1": "value1"
}
],
"insuranceContractLayer": [
{
"id": 1,
"key1": "value1",
"layerInfo__id": 1,
"layerInfo__name": "test",
"layerInfo__key1": "value1",
"otherKey__id": 1,
"otherKey__name": "test",
"otherKey__key1": "value1"
},
{
"id": 2,
"key1": "value2",
"layerInfo__id": 2,
"layerInfo__name": "test2",
"layerInfo__key1": "value2",
"otherKey__id": 2,
"otherKey__name": "test2",
"otherKey__key1": "value2"
}
]
}
Upvotes: 1